c# - Automatic URL Parameter Encoding Failing -


background

in homecontroller.cs have:

[httpget] public getperson(string name) {     return view(new personmodel { ... }); } 

in global.asax.cs have:

public static void registerroutes(routecollection routes) {     routes.ignoreroute("{resource}.axd/{*pathinfo}");      routes.maproute(         "word", "person/{name}",          new { controller = "home", action = "getperson" });      routes.maproute(         "default", "{controller}/{action}",         new { controller = "home", action = "index" }); } 

in somepage.cshtml have, effectively, this:

@{ var name = "winston s. churchill"; } <a href="@url.action("getperson", "home", new { name })">@name</a> 

problem

if click link winston s. churchill, routed url http://localhost/person/winston%20s.%20churchill, yields standard 404 page:

http error 404.0 - not found

the resource looking has been removed, had name changed, or temporarily unavailable.

this happens if name variable contains . (period). code works fine when name is, example, winston churchill.

how can make asp.net mvc 3 percent-encode . (period) in url?

or, how can make routing work without . (period) being percent-encoded?

unacceptable workaround (if presented without justification)

if change route following, works.

routes.maproute(     "word", "person",      new { controller = "home", action = "getperson" }); 

however, url becomes http://localhost/person?name=winston%20s.%20churchill, isn't want. want name in path part of url, not query.

routes contain period , unknown extension interpreted iis static files , not sent through .net pipeline. example, url cite interpreted static file %20churchill extension.

you can force asp.net handle requests adding web.config:

<system.webserver>   <modules runallmanagedmodulesforallrequests="true" /> </system.webserver> 

you'll need this, handle name values end period (as opposed containing one):

<system.web>     <httpruntime relaxedurltofilesystemmapping="true" /> </system.web> 

all /person/{name} urls picked asp.net code.

if rather not use setting, easiest workaround use custom encoding:

name.replace(".","--") 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -