【问题标题】:Breaking my head to get Url Routing in IIS 7 hosting environment : ASP.NET打破我的脑袋在 IIS 7 托管环境中获取 URL 路由:ASP.NET
【发布时间】:2010-12-09 03:20:51
【问题描述】:

我正在尝试使用 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>

然后在 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")));
}

最后 MyRouteHandler 如下所示:

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

在路由页面上,我按如下方式获取产品 ID

ProductId = (int)HttpContext.Current.Items["Product"];

我的烂摊子到此结束。这在本地运行良好。我已经尝试了一段时间,但到目前为止还没有成功。

我们将深深感谢任何帮助。

谢谢...

【问题讨论】:

  • 你们有没有实施任何重写规则?

标签: c# asp.net url-routing


【解决方案1】:

我关注了这篇文章: How to: Use Routing with Web Forms

在我找到它之前,我在共享主机上遇到了问题,而在本地没有问题。这是我的 web.config。

我的主机使用带有集成管道的 IIS 7,我错过了这个:

<handlers>
    <!---after all the others--->       
    <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>

编辑:根据您的设置和代码,唯一剩下的就是检查您是否在 web.config 中定义了 Routing dll 并部署到您的 bin 目录:

<add assembly="System.Web.Routing, Version=3.5.0.0, 
  Culture=neutral, 
  PublicKeyToken=31BF3856AD364E35"/>

【讨论】:

  • 您好,感谢您的回复。您能说出您对 web.config 所做的更改吗?那真的很有帮助。
  • 我添加了解决问题的方法。文章非常清楚如何根据主机设置设置路由。
  • 您好,再次感谢您的回复。我在 Web.Config 中拥有所有内容,并且我查看了您建议的文章。我如何在 Web.Config 中拥有所有必要的设置。我真的无法弄清楚它为什么这样做。我不知道为什么 IIS 7 在获取这样简单的事情时如此复杂,因为如今 URl 路由或重写非常普遍。
  • 谢谢,缺少 urlroutinghandler - 救了我一个头疼的朋友
【解决方案2】:

在你的 web.config 中试试这个。为我工作。

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

【讨论】:

  • 在从本地开发者上传网站到 webhost 后,在路由上得到 404。 runAllManagedModulesForAllRequests 解决了这个问题。
【解决方案3】:

不确定您是否能够找出问题所在...但是,如果您仍在寻找解决方案,那么您可以尝试以下方法。前段时间我不得不面对同样的情况,并使用 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>

如果您在理解重写机制时遇到问题,我建议您阅读 Scott Guthrie 的 this article

我认为在 IIS 7.0 或 7.5 环境下这应该适合您。

【讨论】:

  • 是的,我很久以前就发现了这个问题,并使用重写地图本身修复了它......无论如何感谢您的回复
  • user153410,我也有同样的问题。你能建议你如何解决这个问题吗?
【解决方案4】:

只是为了告知我最终的解决方案是什么...在 IIS7 上将管道模式更改为集成,然后我从上面的链接在 web.config 上添加了一些行... http://msdn.microsoft.com/en-us/library/cc668202.aspx

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多