简体   繁体   English

带可选参数的MVC操作 - 哪个更好?

[英]MVC Action with Optional Parameters — which is better?

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
    }
}

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 如果没有发送参数( GET /somecontroller/action ),则x参数的值在操作中将为null
  • If ax 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 如果发送ax参数,但它不是有效整数( GET /somecontroller/action?x=abc ),则x参数的值在操作中将为null,并且modelstate将无效
  • If ax parameter is sent and the value represents a valid integer ( GET /somecontroller/action?x=123 ), then x will be assigned to it. 如果发送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 x was a route parameter. 在我的示例中,我使用了查询字符串参数的GET请求,但显然同样适用于其他HTTP谓词,如果x是路由参数。

You only need to specify the optional parameter value if it is going to be anything else other than null . 您只需要指定可选参数值,如果它将是除null之外的任何其他null

MVC3 will automatically set null as the value of your parameter if nothing is specified in the overload, or in the call to the Action. 如果在重载或调用Action中没有指定任何内容,MVC3将自动将null设置为参数的值。

However, its worth noting that if there are any non-optional parameters after this parameter in the signature, then null would have to be specified in the call. 但是,值得注意的是,如果签名中的此参数后面有任何非可选参数,则必须在调用中指定null

Therefore its best to put all optional params at the end of the signature. 因此,最好将所有可选参数放在签名的末尾。

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 RequiresRouteValuesAttribute action method selector. 这当然是可行的,只要您为RequiresRouteValuesAttribute操作方法选择器提供非常简单的代码即可。 You can find code in this blog post that does exactly this. 您可以在此博客文章中找到完全相同的代码。

By my opinion this is the best possible solution to this problem, because: 我认为这是解决这个问题的最佳方法,因为:

  1. It simplifies code by removing unnecessary branch 它通过删除不必要的分支简化了代码
  2. Makes code easier to maintain (due to lower complexity) 使代码更易于维护(由于复杂性较低)
  3. Extends Asp.net MVC framework as it can and should 尽可能地扩展Asp.net MVC框架
  4. Keeps parameter types as they should be without the need to make them nullable 保持参数类型应该是它们而不需要使它们可为空
  5. etc. 等等

Anyway. 无论如何。 All the details about this technique is explained in great detail in linked post. 关于此技术的所有细节都在链接的帖子中详细解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM