Is there a way to cast parameter in the controller's action from one type to another in ASP.NET MVC 5?
Example. Action method is the following:
public string Test(Guid input) {
return "";
}
If the method is invoked with parameters "input=hello" then I get the error with the message: "The parameters dictionary contains a null entry for parameter 'input' of non-nullable type 'System.Guid' for method 'System.String Test(System.Guid)' in 'RealtyGuide.WebSite.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
What I want is try to cast the parameter to Guid according to specific (custom) rules. Is it a question on model binding? What are the possible ways to solve this task? Sorry for my English.
About the answers. If you just want assign null to a Guid parameter if it is invalid, then look at this answer. If you are looking for an example of custom model binder, then look at this and this answers.
I have stumbled upon this question when searching about
Guid
binding in action with a graceful fallback when the value is not valid. If the actual provided value does not have to be known, using anullable Guid
(Guid?
) should to the trick. E.g. (this is tested in ASP.NET Web API):If a valid
Guid
is provided, input will be populated. Otherwise, it will benull
.According to the comments from @AndreiV and @AntP the decisions are:
if the string is a correct Guid-string then it is binded automatically (nothing else is needed),
if the string is not a correct Guid-string one should
2.1. make conversion in the action's body (to my mind it entails code duplication), or
2.2. set up the custom (user-defined) model binder. The following is the code for the last approach (model binder).
Registration in Application_Start is necessary:
One may use attributes instead of registration of the binder globally (see here), but I'll not use it, because it entails unnecessary code duplication in my task.
Refs: 1, 2, 3
Trying input="helo" is an wrong method. Guid must contain 32 bit number value example (12345678910111213141516171819202)
Try this input = 12345678910111213141516171819202, By giving value like 32 digit number the Guid value accepts it. then the the method will not display the same error.
in reply to Hoborg's example, https://stackoverflow.com/posts/26676337/revisions the following will return null values or the Guid parameter, if it is desired.
binding as follows in your controller