ASP.NET MVC路由Maproute参数

时间:2022-03-12 17:30:10

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

我正在进行MVC路由框架工作。我对RouteCollection.MapRoute方法采用的参数感到有点困惑。

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. 为什么我们将它用作新的{controller =“Home”,action =“Index”,id =“”}

  3. Is this is a fixed format?
  4. 这是固定格式吗?

  5. How can CLR interpret sequence of parameter?
  6. CLR如何解释参数序列?

Follow up questions:

跟进问题:

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

一个。为什么我们在一个括号{}内装饰控制器,动作或id?

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

湾URL指定和默认值之间是否有匹配?

2 个解决方案

#1


3  

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

为什么我们将它用作新的{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.

基本上用默认值初始化路线。传递给MapRoute方法的值转换为IDictionary 供以后查找。 ,object>

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?

CLR如何插入参数的顺序?

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

通过将它们添加到Dictionary中(准确地说是RouteValueDictionary)。

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

一个。为什么我们在一个括号{}内装饰控制器,动作或id?

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

简短的回答,这是一个常规......很长的答案,根据MSDN,摘自MSDN文章

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.

在URL模式中,您可以通过将它们括在大括号({和})中来定义占位符。您可以在段中定义多个占位符,但它们必须用文字值分隔。例如,{language} - {country} / {action}是有效的路由模式。但是,{language} {country} / {action}不是有效模式,因为占位符之间没有文字值或分隔符。因此,路由无法确定将语言占位符的值与国家/地区占位符的值分开的位置。

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.

是的,默认值是URL中的占位符。如果添加更多默认值然后添加占位符,它将被忽略。如果URL中未包含该参数的值,则使用默认值。

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

有关详细信息,我将向您指出这篇优秀的MSDN文章。

Hope this helps.

希望这可以帮助。

#2


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?

新表达式不适用于MVC 5约束吗?

#1


3  

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

为什么我们将它用作新的{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.

基本上用默认值初始化路线。传递给MapRoute方法的值转换为IDictionary 供以后查找。 ,object>

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?

CLR如何插入参数的顺序?

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

通过将它们添加到Dictionary中(准确地说是RouteValueDictionary)。

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

一个。为什么我们在一个括号{}内装饰控制器,动作或id?

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

简短的回答,这是一个常规......很长的答案,根据MSDN,摘自MSDN文章

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.

在URL模式中,您可以通过将它们括在大括号({和})中来定义占位符。您可以在段中定义多个占位符,但它们必须用文字值分隔。例如,{language} - {country} / {action}是有效的路由模式。但是,{language} {country} / {action}不是有效模式,因为占位符之间没有文字值或分隔符。因此,路由无法确定将语言占位符的值与国家/地区占位符的值分开的位置。

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.

是的,默认值是URL中的占位符。如果添加更多默认值然后添加占位符,它将被忽略。如果URL中未包含该参数的值,则使用默认值。

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

有关详细信息,我将向您指出这篇优秀的MSDN文章。

Hope this helps.

希望这可以帮助。

#2


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?

新表达式不适用于MVC 5约束吗?