Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to have Guid as an optional parameter in asp.net mvc 3 controller action?

I was trying to have an index action on a controller to optionally take a guid like so:

public ActionResult Index(Guid id = default(Guid))

or like so

public ActionResult Index(Guid id = new Guid())

I was hoping to take advantage of C#'s optional parameters and my routes are also defined optional parameters:

routes.MapRoute(
    "Default", "{controller}/{action}/{id}",
     new { controller = "somecontroller", action = "Index", id = UrlParameter.Optional }

but calling "somecontroller/index" gives following error...

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Index(System.Guid)' in 'Controllers.SomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter

is it just not possible? What am I missing? Thanks

like image 863
andryuha Avatar asked Mar 01 '11 20:03

andryuha


People also ask

Can you make a parameter optional?

By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.

Can we make out parameter optional in C#?

This concept is introduced in C# 4.0. Important Points: You can use optional parameters in Methods, Constructors, Indexers, and Delegates. Each and every optional parameter contains a default value which is the part of its definition.

How do you add optional parameters?

In the following example, I define the second parameter (secondNumber) as optional; Compiler will consider “0” as default value. Optional attribute always need to be defined in the end of the parameters list. One more ways we can implement optional parameters is using method overloading.

Can we have multiple optional parameters in C#?

C# Multiple Optional Parameters If you want to define multiple optional parameters, then the method declaration must be as shown below. The defined GetDetails() method can be accessed as shown below. GetDetails(1); GetDetails(1, "Rohini");


1 Answers

A Guid is not nullable. For example you can't do this

Guid myGuid = null;  // invalid

However, in C# 2, some syntactic sugar was added to make value types nullable (by wrapping them in a Nullable object), like so:

Guid? myGuid = null; // valid.  myGuid will be Nullable<Guid>.

Given that rule, let's take a look at your route:

routes.MapRoute(
    "Default", "{controller}/{action}/{id}",
    new { controller = "somecontroller",
        action = "Index",
        id = UrlParameter.Optional // <-- Optional!!
    });

Since you specified in your route that the id parameter is optional, you must use a type that can be null, or you must omit the parameter completely in your action. So to fix your action, you need to change the Guid id parameter to be Guid? id and it should work. After that, you can check the value to make sure it's not null like so:

public ActionResult Index(Guid? id) {
    if (id.HasValue) {
        // it's not null, so I can call "id.Value" to get the Guid
    }
}
like image 116
Scott Anderson Avatar answered Oct 03 '22 21:10

Scott Anderson