MVC 4默认参数值 [英] MVC 4 Default Parameter Value

查看:276
本文介绍了MVC 4默认参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC 4应用程序,同时定义路由我可以提供的默认参数列表。我应提供什么样的价值为可选参数: UrlParameter.Optional 空字符串的?



例如:



<预类=郎-C#prettyprint-覆盖> 路线。图路线(简单,{控制器} / {行动} / {ID},
新的{控制器=家,行动=索引,ID = UrlParameter.Optional});



<预类=郎-C#prettyprint-覆盖> routes.MapRoute(简单的,{控制器} / {行动} / {ID},
新的{控制器=家,行动=索引,ID =});



ID =和 ID = UrlParameter.Optional 在上面的例子吗?



请注意,某些控制器动作将使用 ID (类型为字符串),有的会参。


解决方案

的区别是微妙的,但几乎不重要



UrlParameter.Optional 表示空将被传递给action方法代替的价值。



ID =表示的(NOT NULL)默认值将被传递给操作方法。



在这两种情况下,不包括在你的路线不会停止MVC框架的id参数从寻找合适的方法。



UrlParameter.Optional 的情况下,你应该让所有听上去很像行动采取的方法一个可空参数



键入事项



您不应适用 ID =来使用整数路线。


In MVC 4 application while defining routing I can provide a list of default parameters. What value shall I provide for optional parameter: UrlParameter.Optional or empty string?

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?

Please note that some controller actions will be using id (of type string) and some will be parameterless.

解决方案

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.

id = "" means a default value of "" (not null) will be passed to the Action Method.

In both cases, not including an id parameter in your route will NOT stop the MVC framework from finding the right method.

In the case of UrlParameter.Optional, you should make all relatable Action Methods take a nullable parameter

Type matters

You should NOT apply id="" to routes that use ints.

这篇关于MVC 4默认参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆