Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13594

Asp.net MVC routing function

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

Answers (2)

smartcaveman
smartcaveman

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:

  1. Uses reflection to get all the controllers in the application project.

  2. 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).

  3. 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.

  4. 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.

  5. 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.

Upvotes: 2

Christian Dalager
Christian Dalager

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

Related Questions