c# - How to pass Configuration to non-controller classes in ASP.NET MVC6 -


i developing application using mvc6. can read json configuration in startup file , dependency inject in controllers.

now, requirements want access configuration inside classes not controllers. can't use constructor di in such cases. these classes reside in folder of mvc6 application not model or controller classes.

what's best way access configuration in such non-controller classes?

thank you.

update:

just clarify - can't use di these classes.

these classes instantiated when needed based on condition , methods called on them. need access json config settings within these classes/methods. so, far know these ways:

  1. make configuration property startup static can startup.configuration["..."].

  2. set static properties of custom class startup after reading json config , access static properties.

are these alternative (other di of course)? there other ways?

what create register service method one:

 public static void addmytools(this iservicecollection services, iconfiguration config){       //save config in class  } 

in static method store configurations need in static class.

then in startup.cs use namespace of tools in configureservices section add tools this:

 services.addmytools(configuration);  

hope you. did not try it, maybe change have global idea. (i searching , @sam farajpour ghamari give me idea answer)


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -