2/12/2021 0 Comments Asp Net Mvc Controller
By default ASP.Internet MVC produces a control making use of parameter much less constructor and we will not be capable to inject our course into it.Controller reliance injection is certainly one method that allow us do that.To provide our course into ASP.NET MVC control, we can adhere to two methods Overriding DefaultControllerFactory class that comes along with ASP.Internet MVC Construction Implementing IControllerFactory interface methods of ASP.NET MVC Purpose: Controller dependency shot in ASP.NET MVC The objective of addiction shot in MVC controller is definitely to obtain following where we are passing the parent course to the cónstructor of the Controller so that we can use its strategies into activity methods.
In this situation, we are usually making use of GetName() and GetLoginDetails() method of the Parent course in DefaultControllerFactoryMethod method. Asp Net Mvc Controller Code Snippet ThoroughlyLook at below program code snippet thoroughly, In GetControllerInstance method, we are instantiating the Parent course that will be being passed to the Activator.CreateInstance technique only when the control name will be Home normally the default cónstructor of the control is certainly instantiated without any parameter. The ReleaseController technique simply bank checks for the controller and if it can be disposable, telephone calls the Dispose() method. As soon as we have got created above controller factory class, we need to sign-up them into GIobal.asax ApplicationStart occasion. The various other controller doesnt obtain impacted by this as we have got handed the example of the Mother or father class only when controllerName is definitely Home in the DefaultControllerFactoryMethod course declared over. Solution Method 2 - Implementing IControllerFactory interface strategies to attain dependency shot Create a new class that inherits IControllerFactory interface and put into action its methods. In this case we have made MyCustomControllerFactory class that inherits IControllerFactory interface. Very first we possess implemented CreateController method of this interface that assessments for the controller title and return the control type depending on what we have got composed in the switch statement. 3 rd, ReleaseController as explained above just calls the Dispose() method of the controller. ![]() ![]() Hope this write-up was useful, if this helps do talk about this to the societal sites and direct dotnetfunda.com to your buddies and colleagues. Logos, business names used here if any are just for guide purposes and they may end up being respective owners right or trademarks.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |