Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
}
like image 276
Zaid Masud Avatar asked Mar 28 '12 11:03

Zaid Masud


People also ask

Is it mandatory to specify a default value to optional parameter?

Every optional parameter in the procedure definition must specify a default value. The default value for an optional parameter must be a constant expression. Every parameter following an optional parameter in the procedure definition must also be optional.

What is the use of optional parameter?

What are Optional Parameters? By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.

Can parameters be optional?

The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Each optional parameter has a default value as part of its definition.


3 Answers

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.

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.

like image 79
Darin Dimitrov Avatar answered Oct 11 '22 13:10

Darin Dimitrov


You only need to specify the optional parameter value if it is going to be anything else other than 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.

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.

Therefore its best to put all optional params at the end of the signature.

like image 31
Curtis Avatar answered Oct 11 '22 14:10

Curtis


Best Asp.net MVC solution - use action method selector

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

like image 34
Robert Koritnik Avatar answered Oct 11 '22 14:10

Robert Koritnik