What calls Controller class's methods in MVC - asp.net-mvc

A general question in MVC. We know, in order to call a method, an object need to be created for the class containing the method and call then call it with that object.
My question is, in MVC we only define the default controller name and 'ActionResult' in route config.
Now what is creating object for that controller class and calling method(ActionResult) with that object?

It's the MVC framework itself. Based on the route, method etc it's supposed to handle, it will instantiate a particular controller class and invoke the specified method/s for it to handle that route.

Related

MVC asp.net Resource cannot be found

I am new to asp.net MVC. I have added one controller and view and set route to view my index page. But it is giving me following error.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or
one of its dependencies) could have been removed, had its
name changed, or is temporarily unavailable. Please review
the following URL and make sure that it is spelled
correctly.
Requested URL: /
From your comments I take it, that you misunderstood how the basics of MVC work. (You can read it all here: https://www.asp.net/mvc)
By default MVC has a lot of conventions, so it knows how a route maps to a controller amongst other things. When you issue a request to an url, the default implementation of the MvcHandler receives a controller instance from the default ControllerFactory implementation, which has a convention, that a controller must end on the name Controller. That means, that /Home1Controller/ in your example, lets the controller factory look for a controller class named Home1ControllerController, which most likely does not exist.
For completeness, here are the criteria by which thew default controller factory identifies the correct controller:
class scope must be public
class must not be abstract
class must not take generic parameters
class name must end on "Controller"
class must implement IController interface.
So if you keep the default route, which you have and you have created a controller called Home1Controller, then the route on which you can access the Index method is: /Home1/Index.
If your controller would be namend FooController, then the url would be /Foo/

struts2 - How do request parameters get populated to corresponding fields in the action class?

If there is a request parameter 'name' passed to an action, we can receive it in our Action class if we have a field named 'name'. Which interceptor is responsible for doing this ? I looked at the code for ParametersInterceptor, but it only sets the parameters onto the value stack, not in corresponding fields of the action class
but it only sets the parameters onto the value stack, not in
corresponding fields of the action class
There is just a little glitch in your reasoning: The action class is is at the top of the value stack! So com.opensymphony.xwork2.interceptor.ParametersInterceptor is responsible.
As a piece of advice, though, I would suggest that you don't actually have a parameter named "name" on the action class, but rather move such fields from your action class into another class that will serve as your "model". Then, have your action class implement the ModelDriven interface. This will place the model class on the top of the ValueStack instead of the action class instance, and then the "name" parameter will map onto your model instance.
The separation of the model/data concerns into another class from the action/control concerns will make your code more readable and maintainable. Of course, if there is only, say, a single param, then separating it into a separate class would be silly. More than 2 or 3 params, though, and you will benefit from the separation.

Instantiate a viewmodel in an action filter?

Q: How do I make an object that is instantiated inside an action-filter available within the action-method?
Background:
I have numerous forms (among other things) in an MVC web site.
Each has its own viewmodel, which inherits from a base type (FormPage).
My convention for these is to name the viewmodel type as the action-name prepended with "Form". So my ContactUs viewmodel is FormContactUs : FormPage.
A number of base viewmodel properties are set identically for all forms, and I have a generic utility functon that I call inside the action method to do this.
Setting the viewmodel, choosing the type based on the action-name and the naming convention, and setting base properties common to all forms from within an action-filter will make this just a bit DRY-er. My only hurdle appears to be figuring out how to make an object instantiated inside the filter available within the action-method.
Q: How do I make an object that is instantiated inside an action-filter available within the action-method?
You could store it in the HttpContext.Items which is available throughout the entire request lifecycle. This being said, a custom model binder seems more adapted to your scenario than an action filter.

ASP.NET MVC: Uses a delegate field as an action method?

Is it possible in ASP.NET MVC via some extension/override points to allow a "delegate field" to be used as an "action"?
Something like:
using System;
using System.Web.Mvc;
namespace Company.Web.Controllers
{
public class SwitchboardController : BaseController
{
public Func<ActionResult> Index, Admin, Data, Reports;
public SwitchboardController()
{
// Generic views
Index = Admin = Data = Reports =
() => View();
}
}
}
I know I'm a little hell-bent for this one but if this is possible it'd open up many new ways of making actions. You could, for example, have Django-style generic views in MVC with only a single line of code to define the action or have different ways to factor duplicate logic across multiple controllers.
I'm not quiet sure where would be the place to slap this logic into or how much work would be required to alter something so fundamental in the framework.
You will probably have to build your own Controller factory. This class builds controllers, and implements IControllerFactory. You can inherit from DefaultControllerFactory. Override CreateController() to return your own IController.
Register your controller factory in Application_Start() of MvcApplication using this line:
ControllerBuilder.Current.SetControllerFactory(typeof(MyControllerFactory));
In your implementation of IController, override the Execute method. You can use the RequestContext to decide which delegate to invoke. It would probably be easiest to inherit from ControllerBase, and override Execute in there if you don't want to fully implement IController.
The RequestContext passed into Execute carries a RouteData object. This is a dictionary populated by the routing engine that tells you what action should be invoked, and any parameters. You can get the action name like this:
//context is a RequestContext object passed to IController.Execute()
string actionName = requestContext.RouteData.Values["action"];
You could even define your action as a dictionary, and just pull them out once you get the action name.
One last thing, normal action methods return an ActionResult, which the framework uses to decide which view to render. Once you execute your delegates, I think you'll have to set some stuff manually in your special base controller. I'm not exactly sure what to set or how to get your View executed from here without cracking open the MVC source.
Good luck! This looks like an interesting idea.
As you seem to be implementing a BaseController in your code sample, if you override the Execute (from the IController) you'll be able to interpret the request => action however you like.
No, it isn't. The base controller is looking for methods and not for fields to dispatch an action.
EDIT:
Sorry, I was a bit fast and fixed to the standard classes provided.
You can do that but you have to overwrite the Execute Method in your controller or implement and provide your own IActionInvoker to dispatch the action to fields. Look into the post action processing in detail. It explains the dispatching in detail.

How can I generate a URL within a shared (static) method in MVC.NET?

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

Resources