5

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?

1

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot for quick response it helps a lot.Some more quick question on same 1. url: "{controller}/{action}/{searchId}" Why we decorate controller,action or id inside a bracket {} 2. Is there any match in bewteen URL specify and defaults?
I have added your follow up questions in your original question for other readers. Please see my updated answer. By the way did you read the MSDN article? Did you find it useful?
Yes I go through the link and very usefull.TThanks a lot. But I am bit confused about {} URL pattern, I have tried "{controller}/{action}" and "{action}/{controller}" both are working perfectly :)
Yes, It will work in both cases. Your URL pattern just tell how many placeholders will be there in the incoming request's url after server name. Position does not matter as long as it is present in URL and defaults has value for corresponding placeholder. I suggest try more and you will get it soon.
@user3886602 - When I get confused with which route will match, I use this tool... haacked.com/archive/2008/03/13/url-routing-debugger.aspx
0
routes.MapRoute(
    name: "Search",
    url: "{controller}/{action}/{searchId}",
    defaults: new {controller = "Account", action = "Login", searchId = UrlParameter.Optional},
    new {id = @"\d{1, 2}" }
);

Does new expression not work for MVC 5 constraints?

1 Comment

This looks like its a comment that would normally be included in the answer (when you have more rep). You ill probably get more traction asking this in its own question referencing the posted answer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.