MVC4: url routing with email as parameter
asp.net-mvc, asp.net-mvc-4, asp.net-mvc-routing, c#
Solution
Try 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 view:
<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
}
Problem
I have this url which works absolutely fine on my VS web server ``` http://localhost:4454/cms/account/edituser/email@domain.com (works) ``` but when I publish this site to IIS7.5 , it simply throws 404. ``` http://dev.test.com/cms/account/edituser/email@domian.com (does not work) ``` but the strange things happens if I remove email address ``` http://dev.test.com/cms/account/edituser/email (works , no 404) ``` but if I change my url with query string it works fine ``` http://dev.test.com/cms/account/edituser?id=email@domain.com (works) ``` here is my route entry ``` 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" } ); ``` weird thing I have noticed is that when server throws 404 error page , it is a standards iis 404 page not the one custom one I have. so I was wondering is there any issue with my route mapping or IIS does not like the url which has email as parameter. but everything works fine with my local dev. Thanks