Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Routing Maproute parameter

I am just going through on MVC routing frame work. I am bit confused on parameter taken by RouteCollection.MapRoute method.

If I took a trditional example of below request

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

 - List item

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

It's mapped with following signatures

public static Route MapRoute(
    this RouteCollection routes,
    string name,
    string url,
    Object defaults
)

My question is regarding "Object defaults" ,

  1. Why we are using it as new { controller = "Home", action = "Index", id = "" }
  2. Is this is a fixed format?
  3. How can CLR interpret sequence of parameter?

Follow up questions:

a. Why we decorate controller,action or id inside a bracket {}?

b. Is there any match in between URL specify and defaults?

like image 245
Bharat Joshi Avatar asked Oct 20 '22 03:10

Bharat Joshi


1 Answers

Why we are using it as new { controller = "Home", action = "Index", id = "" }

To basically initialize the route with default values. The values you pass into MapRoute method transformed into a IDictionary<string, object> for later lookup.

Is this is a fixed format?

No, you can omit or add more values as per your URL. The values that you specify in defaults will be used for lookup.

For example, You can set a route name Search as following:

    routes.MapRoute(
        name: "Search",
        url: "{controller}/{action}/{searchId}",
        defaults: new {controller = "Account", action = "Login", searchId = UrlParameter.Optional}
        );

How can CLR interprt sequenation of parameter?

By adding them into a Dictionary (RouteValueDictionary to be precise).

a. Why we decorate controller,action or id inside a bracket {}?

Short answer, it is a convention... long answer, as per MSDN, excerpts from MSDN Article

In a URL pattern, you define placeholders by enclosing them in braces ( { and } ). You can define more than one placeholder in a segment, but they must be separated by a literal value. For example, {language}-{country}/{action} is a valid route pattern. However, {language}{country}/{action} is not a valid pattern, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the language placeholder from the value for the country placeholder.

b. Is there any match in between URL specify and defaults?

Yes, the defaults values are for the placeholders in the URL. If you add more default then placeholder, it will be ignored. The default value is used if a value for that parameter is not included in the URL.

For more information, I will point you to this excellent MSDN Article.

Hope this helps.

like image 179
SBirthare Avatar answered Nov 11 '22 16:11

SBirthare