views:

122

answers:

2

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
        );
+8  A: 

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.

BC
It sounds like someone needs to add, essentially, a `Trim()` somewhere inside IIS. :)
JYelton
Aren
I agree. How do you get IIS to play nice?
BC
I did some testing and came to the same conclusion. Routes that end in %20 don't even enter the MvcHandler : IHttpHandler. This is def an IIS issue.
jfar
Sadly I do need to differentiate between space and no space so the re-write rule is not going to help me. I will probably do a custom encode to something other than %20 to solve it, but not happy. Thanks BC. I'm going to leave this open for now incase anyone else comes up with something - but I kind of doubt it :-)
Andiih
+1  A: 

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 http://stackoverflow.com/questions/591694/url-encoded-slash-in-url

Also http://stackoverflow.com/questions/2515959/how-to-create-a-uri-instance-parsed-with-genericuriparseroptions-dontcompresspath

This is all only wild guessing but maybe it helps.

Malcolm Frexner
Thanks MF. Useful info there, I'll experiment with the techniques, but its worth noting that its ONLY %20 that breaks it, other escaped characters don't have the same effect.
Andiih