打破我的头,在IIS 7托管环境中获取URL路由:ASP.NET

use*_*410 10 c# asp.net url-routing

我正在尝试使用System.Web.Routing实现ASP.NET URL路由.这似乎在我的本地主机上正常工作但是当我上线时,我收到IIS 7的404错误(找不到文件).仅供参考,主机使用Windows Server 2008 IIS7.

我认为这在处理路由机制方面有所不同.但我无法弄清楚到底发生了什么.以下是我到目前为止所做的设置和更改,以使其工作,并给自己一些信誉,它在本地工作绝对正常.

Web.Config设置

然后我有一个system.webserver部分,它具有以下标记

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="Session" />
      <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
      <add name="UrlRoutingModule"
               type="System.Web.Routing.UrlRoutingModule, 
                   System.Web.Routing, Version=3.5.0.0, 
                   Culture=neutral, 
                   PublicKeyToken=31BF3856AD364E35" />

    </modules>
    <handlers>
      <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>   

</system.webServer>
Run Code Online (Sandbox Code Playgroud)

然后在Application_Start部分中,我定义了一条路由,如下所示:

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes); 
}
void RegisterRoutes(RouteCollection routes)
{               
    routes.Add(
       "MyRoute",
          new Route("ProductDetail/{ProductId}/{ProductName}",
                new MyRouteHandler("~/ProductDetail.aspx")));
}
Run Code Online (Sandbox Code Playgroud)

最后MyRouteHandler看起来如下:

 public IHttpHandler GetHttpHandler(RequestContext requestContext)
 {
     var display = (Page)BuildManager.CreateInstanceFromVirtualPath(
                     _virtualPath, typeof(Page));
     HttpContext.Current.Items["ProductId"] = requestContext.RouteData.Values["Product"]; 
     return display;
 }
Run Code Online (Sandbox Code Playgroud)

在路由页面上,我抓住产品ID如下

ProductId = (int)HttpContext.Current.Items["Product"];
Run Code Online (Sandbox Code Playgroud)

这是我一团糟的结束.这在当地很好用.我已经尝试了一段时间,但到目前为止还没有成功.

任何帮助都将受到极大的赞赏.

谢谢...

NiK*_*NiK 2

不确定您是否能够找出问题所在...但是,如果您仍在寻找解决方案,那么您可以尝试以下操作。一段时间前,我不得不面对同样的情况,并使用 Web 配置中的重写规则让它工作,您不需要任何路由机制。因此,首先我鼓励您删除可能拥有的任何路由设置以及 Global.asax 文件中的代码。

然后在该部分中您可以添加重写规则,如下所示

<rewrite>
    <rewriteMaps>
        <rewriteMap name="map1" defaultValue="(.+)"/>
    </rewriteMaps>
    <rules>
        <rule name="Rewrite rule1 for map1">
        <match url="product/(.+)/(.+)"/>
        <conditions>
            <add input="{map1:{REQUEST_URI}}" pattern="(.+)"/>
        </conditions>
        <action type="Rewrite" url="productdetail.aspx?Product={R:1}" appendQueryString="false" redirectType="Permanent"/>
        </rule>
    </rules>
  </rewrite>
Run Code Online (Sandbox Code Playgroud)

如果您在理解重写机制时遇到问题,我建议您阅读Scott Guthrie 撰写的这篇文章。

我认为这应该适用于 IIS 7.0 或 7.5 环境。