Hello All
I am new with ASP.NET MVC.we create a controller like this 'AdminController' but call it only with the name of Admin. How ASP.NET MVC handle this that we don't need to call controller with full name?
Hello All
I am new with ASP.NET MVC.we create a controller like this 'AdminController' but call it only with the name of Admin. How ASP.NET MVC handle this that we don't need to call controller with full name?
This is handled by the Asp.Net MVC framework using Routes. We define routes in Global.aspx.cs. The route defines which controller to be called and what action to be performed for a given url e.g.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Home", action = "Index", id = ""}
);
Also Asp.net MVC prefers Convention over configuration. So as a convention 'Home' means HomeController.
The controller name you specify in the URL is not the same as the class name, just as the action name is not the same as the actual method that on the controller. There is internal mapping between the controller/action and the class/method that MVC does when determining what code needs to be executed.
The generic mapping rule is:
Admin
) and add the suffix Controller
to it and search for a class with this name (AdminController
).Details
) and search for a method on the controller with the same name (ActionResult Details() {}
).However, currently MVC supports explicit mapping of an action to a method with different name through the ActionName
attribute. Thus, you could have an action called Edit
that is mapped to a method ActionResult EditUser() {}
for example.
It is possible that future versions of MVC could add also a similar ControllerName
attribute that allows explicit mapping of a particular controller name to particular class. (In fact, I hope they do, to help solve the problem with providing different implementations of the same controller name in different areas)