Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a trailing %20 (valid data in this case) kill asp.net mvc routing

Take the following controller action

    public ActionResult NextBySURNAME(int id, string data)
    {
        //code to process the data and edit the id accoringly not written yet
        return RedirectToAction("Edit", new { id = id });
    }

if I call it with /Mycontroller/NextBySURNAME/12/Smith%20Simon

then it works fine (in this case editing record 12) but

/Mycontroller/NextBySURNAME/12/Smith%20

gives me a 404

Now I know that in some cases in my problem domain trailing whitespace is significant, so I don't just want to trim it. So why is this breaking my route ?

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}/{data}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional, data=UrlParameter.Optional } // Parameter defaults
        );
like image 560
Andiih Avatar asked Jul 08 '10 16:07

Andiih


2 Answers

So I did some route debugging and found that routes that end with a space weren't even being evaluated by my MVC app. Therefore, IIS must be handling these requests poorly.

I added a rewrite rule to IIS 7.5 to match trailing spaces and rewrite them as the same url without the space. I'm not satisfied with this solution but haven't been able to find an explanation about why IIS mishandles URLs with trailing spaces.

like image 162
BC. Avatar answered Nov 14 '22 16:11

BC.


I think the way escaped characters are handled is changeable in .NEt 4.0, but I have not tried it myself. See http://msdn.microsoft.com/en-us/library/system.uri.aspx.

Andrews answer to URL-encoded slash in URL

Also How to create a Uri instance parsed with GenericUriParserOptions.DontCompressPath

This is all only wild guessing but maybe it helps.

like image 1
Mathias F Avatar answered Nov 14 '22 18:11

Mathias F