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
Related
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.
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.
Does anyone know why the following in the IResultFilter.OnResultExecuting method does not work?
filterContext.Result = new RedirectResult("http://www.google.com");
I am not looking for answers on how to redirect from within the IResultFilter.OnResultExecuting method as this has already been asked here and to which I've actually provided an answer.
Instead I'm after a reason or logic as to why this doesn't work. It looks like it should work but in Mvc 3 it does not. It would appear that unless you set filterContext.Cancel = true then the original ActionResult is executing regardless. This may be a bug specific to Mvc 3 or it may be intentional. However if it is intentional then why?
That's because you are already to late, the controller has already been called.
The base ActionFilterAttribute class has the following methods that you can override:
OnActionExecuting – This method is called before a controller action
is executed.
OnActionExecuted – This method is called after a controller action is
executed.
OnResultExecuting – This method is called before a controller action
result is executed.
OnResultExecuted – This method is called after a controller action
result is executed.
If you want to redirect the user then you should use: OnActionExecuting which is called before a controller action.
Source: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs
I have a controller action.How can i call action method from another controller in asp.net mvc?
Is this a valid thing to do. Is it possible?
If you're first action is complete then you might redirect to another action.
If there is some common code between these two controllers that you don't want to duplicate then you'd probably look at creating a service which would have this and be called by both actions
I'm creating a Shared (static) method in ASP.NET MVC so it can be used by any controller in my project. This static method needs to generate a URL. I think I need to use System.Web.Mvc.UrlHelper, but I can't figure out how to call it from within a static method. The constructor seems to want a RequestContext. Any ideas?
AFAIK, there is no way to get the "current" RequestContext statically. You'll need to pass in the RequestContext from the controller that's calling it. Any controller can do that by just using this code:
this.ControllerContext.RequestContext