views:

48

answers:

2

Hello everyone

I wonder mapping mechanism from controllers to views. I can not understand how mapping can be possible If we just return value of View() method. Controller class's View() method call overloaded View method with null parameters. But how can be possible to mapping to views with none specified returning value of View() method ?

+2  A: 

The controller knows the action that is being invoked and by convention if you don't specify a view name it will look in Views/ControllerName/ActionName.aspx (.ascx) for a corresponding view. If it doesn't find it will show you a list of searched locations.


Here are more details about how it works:

  1. A request comes in /ControllerName/ActionName
  2. The request is intercepted by the ASP.NET pipeline and the routing engine based on the configuration extracts the tokens. If default routes are configured it extracts controller="ControllerName" and action="ActionName".
  3. The routing engine looks in the controller cache to see if a type corresponding to the name ControllerNameController exists.
  4. If it does exist it is instantiated using a controller factory and the method called ActionName is invoked. If the controller doesn't exist and the default controller factory is used reflection is used to find all types deriving from Controller in all referenced assemblies and those types are cached.
  5. The action is executed and the view engine will look for a template stored using the conventions described previously.
Darin Dimitrov
Still i don't know how can Controller knows the action that is being invoked. So can u write more details ?
Freshblood
The entire Controller paradigm is based on reflecting off of the method names of your Controller class. Therefore, upon execution of an action, it is well known what the current method name (Action) is.
Kirk Woll
You mean that class who calls Controller methods already knows the name of method, right ?
Freshblood
+1  A: 

The controller's action method is invoked by the ASP.NET MVC framework. The routing rules you have global.asax define which URL is mapped to which action method.

Adrian Grigore