[英]MVC 4 Default Parameter Value
In MVC 4 application while defining routing I can provide a list of default parameters. 在MVC 4应用程序中,在定义路由时,我可以提供默认参数列表。 What value shall I provide for optional parameter:
UrlParameter.Optional
or empty string ? 我应该为可选参数提供什么值:
UrlParameter.Optional
或空字符串 ?
Examples: 例子:
routes.MapRoute("simple", "{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional});
routes.MapRoute("simple", "{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = ""});
Is there any difference between id = ""
and id = UrlParameter.Optional
in above examples? 在上面的示例中,
id = ""
和id = UrlParameter.Optional
有什么区别。
Please note that some controller actions will be using id
(of type string
) and some will be parameterless. 请注意,某些控制器动作将使用
id
(类型为string
),而某些动作将是无参数的。
The difference is subtle and but almost unimportant 差异是细微的,但几乎不重要
UrlParameter.Optional
means a null will be passed to the Action Method in lieu of a value. UrlParameter.Optional
表示将空值代替值传递给Action方法。
id = ""
means a default value of "" (not null) will be passed to the Action Method. id = ""
表示将默认值“”(不为null)传递给Action Method。
In both cases, not including an id parameter in your route will NOT stop the MVC framework from finding the right method. 在这两种情况下,在路由中不包括id参数都不会阻止MVC框架找到正确的方法。
In the case of UrlParameter.Optional
, you should make all relatable Action Methods take a nullable parameter 如果是
UrlParameter.Optional
,则应使所有相关的操作方法都采用可为空的参数
Type matters 类型很重要
You should NOT apply id=""
to routes that use ints. 您不应将
id=""
应用于使用int的路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.