Freshblood
Freshblood

Reputation: 6441

About MVC 2 View mapping mechanism

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 ?

Upvotes: 0

Views: 128

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

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.

Upvotes: 2

Adrian Grigore
Adrian Grigore

Reputation: 33318

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.

Upvotes: 1

Related Questions