Maproute ASP.NET MVC Parameter

I am going through MVC routing frame work . I am a little confused about the parameter accepted by the RouteCollection.MapRoute method.

If I accepted the trial example below the 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 is displayed with the following signatures

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

My question is about "Object defaults",

  • Why do we use it as new { controller = "Home", action = "Index", id = "" }
  • Is this a fixed format?
  • How CLR interpret sequence parameter?

Follow up questions:

a. Why do we decorate the controller, action or identifier inside the bracket {}?

b. Is there a match between the url and the default value?

+5
source share
2 answers

Why we use it as a new {controller = "Home", action = "Index", id = ""}

Basically initialize the route with default values. The values ​​that you pass to the MapRoute method, converted to an IDictionary<string, object> for later retrieval.

 Is this is a fixed format? 

No, you can omit or add more values ​​according to your url. The values ​​you specify by default will be used for the search.

For example, you can set the name of the Search route as follows:

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

How can a sequence of CLRs be interprt parameter?

By adding them to the dictionary (RouteValueDictionary will be accurate).

a. Why do we decorate the controller, action or identifier inside the bracket {}?

Short answer, this convention ... long answer, according to MSDN, excerpts from MSDN Article

In the URL pattern, you define placeholders by enclosing them in curly braces ({and also}). 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 a literal value or delimiter between placeholders. Therefore, routing cannot determine where to separate the value for the placeholder language from the value for the placeholder country of the country.

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

Yes, the default values ​​for placeholders in the URL. If you add a default value, the default value will be ignored. The default value is used if the value for this parameter is not included in the URL.

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

Hope this helps.

+3
source
 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 work for MVC 5 restrictions?

0
source

Source: https://habr.com/ru/post/1200720/


All Articles