18

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
        );
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Andiih
  • 12,285
  • 10
  • 57
  • 88

2 Answers2

9

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.
  • 24,298
  • 12
  • 47
  • 62
  • 1
    It sounds like someone needs to add, essentially, a `Trim()` somewhere inside IIS. :) – JYelton Jul 08 '10 at 17:34
  • @JYelton & BC, what if you want to search form something like: `"Hello "` If the trim happens, this would break. The routing should work as expected and produce a string (in OPs case) of `"Smith "` for him to handle later. – Aren Jul 08 '10 at 17:43
  • I agree. How do you get IIS to play nice? – BC. Jul 08 '10 at 17:51
  • 1
    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. – John Farrell Jul 08 '10 at 17:54
  • 1
    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 Jul 08 '10 at 17:57
  • this is also the case with trailing space in any segment. i.e. `http://localhost/Controller/Tag%20/1` also gives 404. Spaces anywhere else, including at the start of a segment are fine. `http://localhost/Controller/%20Tag/1` works. – Kinjal Dixit Jul 22 '13 at 10:08
1

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.

Community
  • 1
  • 1
Mathias F
  • 15,906
  • 22
  • 89
  • 159
  • 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 Jul 09 '10 at 07:19