Reputation: 11427
I'm using ASP.Net MVC 3 and am trying to pass an email address as a parameter in a URL like this:
www.myapp.co.uk/customers/changedetails/[email protected]
The parameter value is null when passed in. If I use parameters then it works;
www.myapp.co.uk/customers/changedetails/[email protected]
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 (www.myapp.co.uk/customers/changedetails/[email protected])?:
Upvotes: 1
Views: 2659
Reputation: 13
You can try add the following code into your web.config.
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
This is because the IIS Express think ".com" as an extension and looks for related module to handle it. However, there isn't related module, so it call the static module to handle it. Then it will search for a static content file path "/customers/changedetails/[email protected]", then it report the 404 not found error. Add above code in your web.config will make your request contain email handled by MVC module instead of StaticFile Module.
Upvotes: 0
Reputation: 5407
Don't have e-mail as last route param. Or if you do, add a trailing slash.
/my/route/[email protected] -> fail
/my/route/[email protected]/ -> success
/my/[email protected]/route -> success
Reasoning behind this is quite complicated but here's a good article about these things http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx
What I am sure of is, that many times if you have e-mail as last without trailing slash, the request won't even reach the asp.net pipeline. It looks like a file request. That being said, It's *.com extension looks indeed very dangerous. Having an @ in the file name certainly does not decrease it's suspiciousness.
You might get it working. You might need to loosen the security in order to do so but it almost certainly will break at some point.
So, the best option is to keep it as query string parameter. Second best option to trail it with a slash.
Upvotes: 7
Reputation: 3052
You need to add another route:
routes.MapRoute(
"ChangeEmail",
"customers/changedetails/{email}",
new { controller = "Customers", action = "ChangeDetails", email = UrlParameter.Optional }
);
The URL parameter name needs to match the action method parameter name.
Upvotes: 1