Reputation: 13594
Can someone please explain what the following function does. I am learning Asp.net MVC and unable to understand which controller is called when and renders which view.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//register custom routes (plugins, etc)
var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>();
routePublisher.RegisterRoutes(routes);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
}
This code is from nopCommerce source-code. I can't understand the URL routing for this project
Upvotes: 2
Views: 1432
Reputation: 42266
The logic for this is in the System.Web.Mvc.MvcHandler
class, the System.Web.Mvc.DefaultControllerFactory
class, and the System.Web.Mvc.ControllerActionInvoker
class. .NET Reflector is your friend.
Basically, the MVC framework:
Uses reflection to get all the controllers in the application project.
Then it does something like IEnumerable<string> controllerNames = controllerTypes.Select(controllerType => controllerType.Name.Replace("Controller",string.Empty));
. It then tries to match the first path segment, {controller}
, to one of these sanitized controller type names (case-insensitive).
Then, it looks at this controller's public methods that have a return type that is of type ActionResult
or some derivative. It matches the method name to the second path segment, {action}
, as the action method to be called.
If the selected method has a parameter that is named id
, then it matches the third path segment {id}
to that value, and passes it to the method. Otherwise, the optional id
parameter is ignored.
If the ActionResult
type that is returned is a derivative of ViewResultBase
then the IViewEngine
tries to locate a corresponding view in the project using whatever conventions have been specified for that view engine. The WebFormViewEngine
, for example, looks in the project for ~/Views/{controller}/{action}.ascx
, ~/Views/{controller}/{action}.aspx
, ~/Views/Shared/{action}.ascx
, ~/Views/Shared/{action}.aspx
by default.
If you want to further understand how routing works in MVC, I would highly suggest Scott Gu's article on MVC Routing.
As far as the IRoutePublisher
method, that looks like a nopCommerce
specific method that automatically registers additional routes specific to nopCommerce
's configuration. If you are interested in how nopCommerce's specific routing conventions work, you can download the source code from the nopCommerce codeplex page and do a search for its default IRoutePublisher
implementation.
IRoutePublisher
is here: http://nopcommerce.codeplex.com/SourceControl/changeset/view/7e34dd9d98f3#src%2fPresentation%2fNop.Web.Framework%2fMvc%2fRoutes%2fRoutePublisher.cs . Basically, it gets all implementations of IRouteProvider
and registers their route definitions in order according to their priority.Upvotes: 2
Reputation: 6643
nopCommerce employs a loosely coupled infrastructure that registers routes for each plugin separately.
So If you need to understand what's going on, check the nopCommerce source code and look for RouteProvider classes, that each plugin has. They are dynamically loaded on application start.
If you need to create your own routes, you can still do that the traditional way -- but be aware, that there might be some clashes.
(Disclaimer: I just looked at the source code, don't know anything else about it).
Upvotes: 2