开发者

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

开发者 https://www.devze.com 2023-01-06 07:07 出处:网络
Take the following controller action 开发者_运维问答 public ActionResult NextBySURNAME(int id, string data)

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
        );


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号