StructureMap MVC 5 html.Action Issue - dependency-injection

I am trying to call an Action from my view using #Html.Action("ActionName","controllerName"). But my page fails to load with below error:
A single instance of controller 'Web.Areas.Area1.Controllers.ActionController'
cannot be used to handle multiple requests. If a custom controller
factory is in use, make sure that it creates a new instance of the
controller for each request.
I am using structure map for Dependency injection. Please help me what am i missing.

You need to add
x.For<{Your controller name}>().AlwaysUnique();
in IoC.cs file. This should be done for every controller in your project.
For more details check this link.

Related

Numerous MVC controller instantiations of the same controller

I'm novice in MVC with c# and I'm wondering why the controller instance constructor will be called frequently (that means more than once).
I have created a typical view based on a layout page. Within the layout page, I'm calling a Kendo TreeView.I'm also using AutoFac and I have added the following instruction to my Global.asax according to AutoFac MVC instruction:
builder.RegisterControllers(typeof(MvcApplication).Assembly);
If I'm debugging, the controller instance constructor which is using the view from above is getting called as soon as the Kendo TreeView is being processed. I can understand if the controller action is called for providing the Kendo TreeView datasource, but I'm not understanding the several calls of this instance constructor. I assumed that there is already an instance of the controller.
Does anyone know if a numerous instance constructor execution of the same controller is normal or is there an error in my coding in general, for instance, I should maybe not place the Kendo Treeview in a layout, maybe in a partial view?
Yours
Stephan
Thanks to Stephen and NightOwl888. I will avoid now creating new instances of other objects within controller's constructor method.
Is it in general a good idea to use AutoFac and register the Controller class as Singleton in order to reuse already created controllers (maybe because I have properties which I won't to reload again) ?

How do I call a method on one controller from another in .NET 4 MVC

In an asp.net MVC application, I need to produce some documens, HTML and PDF, which are not sent to the user's browser, but either sent by mail or entered in our document journalizing system. I produce these documents using Razor.
When a document is used only once, I just add a method to the relevant controller, and the view to that controller's view folder. This works. But I have a document that must be produced at two places in the application, implemented in separate controllers. I have made a new controller for this document with its own view folder.
My question is now: how do I call a method on this controller? Searching the web gives many answers, but all redirect the user to this document, which is not what I need.
You can just call it like you would any other method e.g.
public ActionResult DoSomething()
{
// Some code
var otherController = new OtherController(); // The other controller where the method is
otherController.CreatePdf(); // Call the method
// Continue with what ever else you need to do
return View(); // This will then return the `DoSomething` View
}
But personally it doesn't seem like this logic belongs in a controller. You should possibly think about refactoring this logic out of a controller and into a more logical place. Possibly create your own document generation class and use that.
If I'm getting you right.You could create a base controller and add the method there. You can inherit the Base controller in any controller where you want to call the method. here's a link that might help show you the use of Base controllers. How to wire common code from a base controller in ASP.NET MVC.

Default method in Controllers for MVC framework

Is there any default method that we can set in MVC controllers? I am trying to debug a code and one method is being called (public HttpResponseMessage GetBySpecification) for the first time when controller is called; from where this method is passed I am not able to figure that. URI and get is being passed to controller.
Please help

Grails Custom Scaffolding get access to controller name

I am trying to write a custom src/templates/scaffolding/Controller.groovy and was wondering if there was any way to get access to the controller name? Right now it seems like you can only get the "model" class. The reason I need it is I am customizing the render to prefix the templates directory based on the controller.
For instance I have a controller named AuthorAdminController and I need to customize the list to use the /admin/user/** directory.
Let me know if you have any questions. I am getting ready to look into how to customize DefaultGrailsTemplateGenerator but I am not sure if that is the correct route to go.
Example:
class UserAdminController {
static scaffold = User
}
Currently in my Controller.groovy I get className='user' so I have no access to the controller.
I don't think you can, as the way scaffolding works your template will always be generating a class named DomainClassNameController (i.e. UserController in your example), which gets loaded into a new classloader and then the metaclass of the real controller (UserAdminController) gets new actions added to it which delegate to an instance of the generated UserController.
Now every controller has access to the controllerName property during execution of actions, so this may provide you with a workaround. I haven't tried it, but you could try putting a log.info("controller: \${controllerName}") into the template and see which name it gives you (the backslash to make it resolve at runtime rather than generation time).

Are ASP.NET controllers guaranteed to be used only once per request?

If we want to create some objects to be used by all action methods in a controller, can we store them as instance variables in the controller?
Phil Haack mentions that controllers are not meant to be reused in this old post:
Asp.Net MVC Beta: Previous RouteData overrides current RouteData?
But is this one-controller-per-request behavior guaranteed?
I don't want to be in a situation where a reused controller has data from another request.
Yes (to the question in your title) and No (to the question in your post).
A new controller instance is created for each request. You might be able to do this by using your own controller factory that caches controllers and only creates them as needed, but I wouldn't recommend it. You'd be better off to simply cache any information that is needed either in the Session or the Cache or store the information in the View (hidden, if necessary) than to go to the trouble of creating a new controller factory.
Every time you call an action method, a new instance of the controller class should be created. So it is not a good idea to have your action methods depend on instance variables. If you use the DefaultControllerFactory then you will get a new controller instance for each request. If you use a custom controller factory you could override this behavior, which I wouldn't recommend.
You can use TempData to store temporary data in your controller

Resources