MVC4: url routing with email as parameter

泪湿孤枕 提交于 2019-12-10 16:57:59

问题


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


回答1:


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
}



回答2:


email@domain.com contains characters which are not a legal part of the path (for good reason, they are often reserved for special functions). You'll need to quote/url encode these characters or pass them in the querystring.

Scott Hanselman has a great post on allowing "Naughty Things" in urls.



来源:https://stackoverflow.com/questions/15978555/mvc4-url-routing-with-email-as-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!