Breaking my head to get Url Routing in IIS 7 hosting environment : ASP.NET

asp.net, c#, url-routing

Solution

Not sure if you were able to figure out what the problem was...however if you are still looking for a solution then you may try the following. I had to face the same situation some time back and got it to work using Rewrite rules in Web config for which you will not need any routing mechanism. So first I would encourage you to remove any routing setting you may have and the code from the Global.asax file too.

Then in the section you can add as rewrite rules as follows

<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>

If you have problems understanding the rewrite mechanism I would recommend that you read this article by Scott Guthrie.

I think this should work for you given a IIS 7.0 or 7.5 environment.

Problem

I am trying to implement ASP.NET URL routing using the System.Web.Routing. And this seems to work fine on my localhost however when I go live I am getting an IIS 7's 404 error (File not found). FYI the hosting uses Windows Server 2008 IIS7. I think this is making some difference in handling the routing mechanism. But I am not able to figure out whats exactly happening. Below are the settings and changes that I've made so far to get it work and to give some credit to myself it works absolutely fine locally. Web.Config Settings And then I have a system.webserver section that has the following markup ``` <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> ``` Then in the Application_Start section I have defined one route as follows: ``` 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"))); } ``` Finally MyRouteHandler looks as follows: ``` public IHttpHandler GetHttpHandler(RequestContext requestContext) { var display = (Page)BuildManager.CreateInstanceFromVirtualPath( _virtualPath, typeof(Page)); HttpContext.Current.Items["ProductId"] = requestContext.RouteData.Values["Product"]; return display; } ``` And on the routed page I am grabbing the product ID as follows ``` ProductId = (int)HttpContext.Current.Items["Product"]; ``` And this is the end of my mess. And this works fine locally. I have been trying this for a while but didn't succeeded so far. ANY HELP WILL BE DEEPLY APPRECIATED. Thanks...

Original source