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.