asp.net mvc does not receive the GET request with

2019-04-26 19:49发布

I'm using .net4.5rc with MVC4.0rc

The code below is taken from a MVC webapi application but I the same behaviour is there for reular asp.net mvc

My registerroutes code looks like this

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapHttpRoute(
                name: "R1",
                routeTemplate: "product/{id}",
                defaults: new { 
                                controller="Product",
                                id = RouteParameter.Optional 

                              }
            );

This works well with Ids that doesn't have a period in them. However when I ask for a product with a period in it, the call does not seem to get to ASP.NET

when I use the setting below as suggested on similar posts it works when there is a trailing / But it doesn't work without it

in summary

http://mysite.com/product/AZ0     //works
http://mysite.com/product/AZ.0/   //work with relaxedUrlToFileSystemMapping
http://mysite.com/product/AZ.0    //does not work

The response is 404 and I guess this is returned by IIS without involving ASP.NET. When I run routedebugger with a URL like the last one above, I don't even get the routedebugger stuff

How do I make IIS to pass the control to ASP.NET when there is a URL with a pattern: mysite.com/product/{id} regardless of whether there is a period in the id or not.

thanks.

1条回答
干净又极端
2楼-- · 2019-04-26 20:44

Let me answer this myself. Adding a URL Rewrite solves the problem. The code below added to the web.config tells to rewrite the Url with a trailing "/" if the URL is of the form (product/.) Then it is no longer handled as a file but handled as regular MVC.

  <system.webServer>
   ....   
    <rewrite>
      <rules>
        <rule name="Add trailing slash" stopProcessing="true">
          <match url="(product/.*\..*[^/])$" />
          <action type="Rewrite" url="{R:1}/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
查看更多
登录 后发表回答