简体   繁体   English

MVC4:使用电子邮件作为参数进行URL路由

[英]MVC4: url routing with email as parameter

I have this url which works absolutely fine on my VS web server 我有这个网址在我的VS网络服务器上运行得非常好

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

but when I publish this site to IIS7.5 , it simply throws 404. 但是当我将这个站点发布到IIS7.5时,它只会抛出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. 我注意到的奇怪的事情是当服务器抛出404错误页面时,它是标准iis 404页面而不是我自己的一个自定义页面。
so I was wondering is there any issue with my route mapping or IIS does not like the url which has email as parameter. 所以我想知道我的路由映射有什么问题,或者IIS不喜欢有电子邮件作为参数的网址。 but everything works fine with my local dev. 但是我的当地开发人员一切正常。
Thanks 谢谢

Try url encoding the email address in the URL. 尝试使用URL编码URL中的电子邮件地址。

EDIT 编辑

UrlEncode + Base64? 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
}

email@domain.com contains characters which are not a legal part of the path (for good reason, they are often reserved for special functions). email@domain.com包含的字符不是路径的合法部分(有充分理由,它们通常保留用于特殊功能)。 You'll need to quote/url encode these characters or pass them in the querystring. 您需要引用/ url编码这些字符或在查询字符串中传递它们。

Scott Hanselman has a great post on allowing "Naughty Things" in urls. 斯科特·汉塞尔曼(Scott Hanselman)在网址上发表了一篇关于允许“顽皮的事情”的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM