打破我的头,在IIS 7托管环境中获取URL路由:ASP.NET
-
05-07-2019 - |
题
我正在尝试使用 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"];
这是我一团糟的结束。这在当地很好用。我已经尝试了一段时间但到目前为止没有成功。
任何帮助都会受到极大的赞赏。
...谢谢
解决方案
不确定您是否能够找出问题所在...但是如果您仍在寻找解决方案,那么您可以尝试以下方法。我不得不在一段时间内面对相同的情况并使用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撰写。
我认为这应该适用于IIS 7.0或7.5环境。
其他提示
我遵循了这篇文章: 如何:使用网络表单路由
在我发现它之前,我在共享主机上遇到了问题而在本地没有问题。这是我的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中尝试此操作。为我工作。
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
只是为了告知最终我的解决方案...在IIS7上将管道模式更改为Integrated,然后我在上面的链接中添加了web.config上的一些行... http://msdn.microsoft.com/en-us/library/cc668202。 ASPX
祝你好运。