C# 带有可选参数的 MVC 操作——哪个更好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9906191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC Action with Optional Parameters -- which is better?
提问by Zaid Masud
Are there any pros/cons of using the following two alternatives in your action signature:
在您的动作签名中使用以下两种替代方案是否有任何优点/缺点:
public ActionResult Action(int? x) // get MVC to bind null when no parameter is provided
{
if(x.HasValue)
{
// do something
}
}
OR
或者
public ActionResult Action(int? x = null) // C# optional parameter (virtual overload)
{
if(x.HasValue)
{
// do something
}
}
采纳答案by Darin Dimitrov
I have never seen the second action signature in practice and can't see any usefulness of it.
我在实践中从未见过第二个动作签名,也看不出它有什么用处。
The first one usually covers all the scenarios:
第一个通常涵盖所有场景:
- If no parameter is sent (
GET /somecontroller/action), the value of the x argument will be null inside the action - If a x parameter is sent, but it is not a valid integer (
GET /somecontroller/action?x=abc), the value of the x argument will be null inside the action and the modelstate will be invalid - If a x parameter is sent and the value represents a valid integer (
GET /somecontroller/action?x=123), then x will be assigned to it.
- 如果没有发送参数 (
GET /somecontroller/action),则 x 参数的值在动作中将为空 - 如果 ax 参数被发送,但它不是一个有效的整数 (
GET /somecontroller/action?x=abc),则 x 参数的值在动作中将为空,并且模型状态将无效 - 如果 ax 参数被发送并且该值代表一个有效的整数 (
GET /somecontroller/action?x=123),那么 x 将被分配给它。
In my examples I have used GET requests with query string parameters but obviously the same applies with other HTTP verbs and if xwas a route parameter.
在我的示例中,我使用了带有查询字符串参数的 GET 请求,但显然这同样适用于其他 HTTP 动词,如果x是路由参数。
回答by Curt
You only need to specify the optional parameter value if it is going to be anything else other than null.
如果它不是null.
MVC3 will automatically set nullas the value of your parameter if nothing is specified in the overload, or in the call to the Action.
null如果在重载或对 Action 的调用中未指定任何内容,MVC3 将自动设置为您的参数值。
However, its worth noting that if there are any non-optional parameters after this parameter in the signature, then nullwould have to be specified in the call.
但是,值得注意的是,如果签名中此参数之后有任何非可选参数,则null必须在调用中指定。
Therefore its best to put all optional params at the end of the signature.
因此最好将所有可选参数放在签名的末尾。
回答by Robert Koritnik
Best Asp.net MVC solution - use action method selector
最佳 Asp.net MVC 解决方案 - 使用操作方法选择器
Why not simplify controller action methods by removing unnecessary code branch and have this kind of code as seen here:
为什么不通过删除不必要的代码分支来简化控制器操作方法,并使用这种代码,如下所示:
public ActionResult Index()
{
// do something when there's no id
}
[RequiresRouteValues("id")]
public ActionResult Index(int id)
{
// do something when id is present
}
This is of course possible, as long as you provide the very simple code for RequiresRouteValuesAttributeaction method selector. You can find code in this blog postthat does exactly this.
这当然是可能的,只要您为RequiresRouteValuesAttribute操作方法选择器提供非常简单的代码。您可以在此博客文章中找到完全执行此操作的代码。
By my opinion this is the best possible solution to this problem, because:
我认为这是解决此问题的最佳解决方案,因为:
- It simplifies code by removing unnecessary branch
- Makes code easier to maintain (due to lower complexity)
- Extends Asp.net MVC framework as it can and should
- Keeps parameter types as they should be without the need to make them nullable
- etc.
- 它通过删除不必要的分支来简化代码
- 使代码更易于维护(由于较低的复杂性)
- 尽可能扩展 Asp.net MVC 框架
- 保持参数类型为它们应该的样子,而不需要使它们可以为空
- 等等。
Anyway. All the details about this technique is explained in great detail in linked post.
反正。有关此技术的所有细节都在链接帖子中详细解释。

