9

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.

4

1 回答 1

9

让我自己回答这个问题。添加 URL 重写解决了这个问题。下面添加到 web.config 中的代码告诉如果 URL 的格式为 (product/ ),则用结尾的“/”重写 Url 然后它不再作为文件处理,而是作为常规 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>
于 2012-07-31T09:25:34.167 回答