我正在尝试使用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,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阅读this article.
我认为这应该适用于您,因为IIS 7.0或7.5环境.