I’m using ASP.Net MVC 3 and am trying to pass an email address as a parameter in a URL like this:
http://www.myapp.co.uk/customers/changedetails/john@doe.com
The parameter value is null when passed in. If I use parameters then it works;
http://www.myapp.co.uk/customers/changedetails/?email=john@doe.com
My controller looks like this:
public class CustomerController {
[HttpGet]
public ViewResult ChangeDetails(string email)
{
var model = GetModel(email);
return View(model);
}
}
My register routes looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
What am I missing to be able to use the email as the {id} parameter (http://www.myapp.co.uk/customers/changedetails/john@doe.com)?:
You need to add another route:
The URL parameter name needs to match the action method parameter name.