Search code examples
c#asp.net-mvcasp.net-mvc-4asp.net-mvc-routing

I cannot figure out RouteConfig.cs file


I am very new to MVC and trying to build my first website. I couldnt set my RouteConfig file properly. I have 2 rules that apply to different ActionResults. But, only one of them works properly. if GetProducts is above the GetProductByCode, then GetProducts works. If GetProductByCode is above the GetProducts, then GetProductByCode works. What am I doing wrong?

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "GetProducts",
            url: "{controller}/{action}/{PageNo}",
            defaults: new { controller = "Home", action = "GetProducts", PageNo = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "GetProductByCode",
            url: "{controller}/{action}/{ProductCode}",
            defaults: new { controller = "Home", action = "GetProductByCode", ProductCode = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

My Solution is like below

routes.MapRoute(
            name: "GetProducts",
            url: "{controller}/GetProducts/{PageNo}",
            defaults: new { controller = "Home", action = "GetProducts", PageNo = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "GetProductByCode",
            url: "{controller}/GetProductByCode/{ProductCode}",
            defaults: new { controller = "Home", action = "GetProductByCode", ProductCode = UrlParameter.Optional }
        );

Solution

  • If you look at the default route:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    

    Then think of the portion after url: as a format or pattern:

    • {controller}
    • {action}
    • {id}

    Your 3 URLs Home/GetProducts, Home/GetProductsByCode and Home/Index all match this pattern.

    With the {action} portion being GetProducts, GetProductsByCode and Index respectively.

    You would need to leverage routing if you wanted to map the parameter to a variables called PageNo or ProductCode in the Action, but in general you don't need routes for each possible combination. If your parameters in these actions are id then it will just work without you needing to create routes for each.

    E.g.

    public ActionResult GetProducts(int id) 
    {
     // stuff
    }
    
    public ActionResult GetProductsByCode(string id)
    {
     // stuff
    }
    

    To have the parameter names, specify the controller and action explicitly:

       routes.MapRoute(
            name: "GetProducts",
            url: "Home/GetProducts/{PageNo}",
            defaults: new { controller = "Home", action = "GetProducts", PageNo = UrlParameter.Optional }
        );
    
        routes.MapRoute(
            name: "GetProductByCode",
            url: "Home/GetProductsByCode/{ProductCode}",
            defaults: new { controller = "Home", action = "GetProductByCode", ProductCode = UrlParameter.Optional }
        );
    

    And

    public ActionResult GetProducts(int PageNo) 
    {
     // stuff
    }
    
    public ActionResult GetProductsByCode(string ProductCode)
    {
     // stuff
    }
    

    But in general, only define custom routes that differ from the normal {controller}/{action}/{id} pattern.


    The default section of MapRoute means that if it can't find a controller and action that exists in your code base use these instead. It's a fallback, not the functionality driver.