MVC4: Routing URLs Using Email as a Parameter

I have this url that works fine on my VS web server

http://localhost:4454/cms/account/edituser/ email@domain.com (works) 

but when I publish this site in IIS7.5, it just produces 404.

 http://dev.test.com/cms/account/edituser/ email@domian.com (does not work) 

but strange things happen if I delete the email address

 http://dev.test.com/cms/account/edituser/email (works , no 404) 

but if I change my url with the query string it works fine

 http://dev.test.com/cms/account/ edituser?id=email@domain.com (works) 

here is my route record

 routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults new[] { "App.NameSpace.Web.Controllers" } ); 

The strange thing I noticed is that when the server issues a 404 error page, this iis 404 standards page is not the one I have.
so I was wondering if there are any problems with my route mapping or IIS doesn't like the url that has the email address as a parameter. but everything works fine with my local developer.
thanks

+4
source share
2 answers

Try the URL encoding the email address in the URL.

EDIT

UrlEncode + Base64?

  public static string ToBase64(this string value) { byte[] bytes = Encoding.UTF8.GetBytes(value); return Convert.ToBase64String(bytes); } public static string FromBase64(this string value) { byte[] bytes = Convert.FromBase64String(value); return Encoding.UTF8.GetString(bytes); } 

In your opinion:

 <a href="@Url.Action("MyAction", new { Id = email.ToBase64() })">Link</a> 

In your controller

 function ActionResult MyAction(string id){ id = id.FromBase64(); //rest of your logic here } 
+7
source

email@domain.com contains characters that are not the legal part of the path (for good reason, they are often reserved for special functions). You will need to tell / url to encode these characters or pass them in the query string.

Scott Hanselman has a great post allowing "Naughty Things" in URLs.
+4
source

All Articles