简体   繁体   中英

MVC4: url routing with email as parameter

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

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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