【发布时间】: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