ASP.NET MVC: How to Route Search Term with . (Peri

2019-01-09 13:09发布

问题:

I get a 404 response from .Net MVC when I try to make a request where my search term ends with a . (period). This is the route that I'm using:

routes.MapRoute(
                "Json",
                "Remote.mvc/{action}/{searchTerm}/{count}",
                new { controller="Remote", count=10}
            );

The search works fine with a . inside the search term, it just cannot end with it. Any thoughts on how to route this search request?

回答1:

I have solved a similar issue (I had trouble with paths like /music/R.E.M.) I've added the following line into the system.webServer/handlers section (adjusted for your case):

 <add name="UrlRoutingHandler" type="System.Web.Routing.UrlRoutingHandler, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" path="Remote.mvc/*" verb="GET"/>

I've noted also, that

<httpRuntime relaxedUrlToFileSystemMapping="true" />

does work only if the period (.) is somewhere in the middle pair of slashes (e.g. /abc/d.e/f) and does not work when the period looks like a file type separator (e.g. /abc/de/f.g).



回答2:

If you are using .NET 4.0, you can set this flag in the system.web section of your web.config and it will be allowed:

<httpRuntime relaxedUrlToFileSystemMapping="true" />

I've tested it and it works. Haack has an explanation of it.