ASP.NET 4.0 URL Routing HTTP Error 404.0 - Not Found
Asked Answered
S

3

13

I have implemented URL routing in ASP.NET 4.0 using following route.

routes.MapPageRoute(
   "NewsDetails",               // Route name
   "news/{i}/{*n}",  // Route URL
   "~/newsdetails.aspx"      // Web page to handle route
    );

which gives me url like

http://www.mysie.com/news/1/this-is-test-news

and this is working in my localhost fine.

But when I uploaded it on the server it gives ...

Server Error

404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, 
or is temporarily unavailable.

If I try http://www.mysie.com/news/1/this-is-test-news.aspx then it displays page.

Has anyone have same problem?

How can i set URL http://www.mysie.com/news/1/this-is-test-news to work on windows server 2008 ?

Suckling answered 19/8, 2010 at 15:15 Comment(2)
Is the file with the route in the same directory as that of the aspx page?Sandisandidge
no. my aspx files are at root folderSuckling
S
34

To enable default ASP.Net 4.0 routing with IIS 7.5:

  1. Make sure that you have installed the HTTP Redirection feature It can be done -> Control Panel -> Progams -> Turn off windows features -> World wide web Services -> Common HTTP Features -> HTTP Redirection
  2. Modify your web.config with the code below

 

<system.webServer>   
    <modules runAllManagedModulesForAllRequests="true">    
        <remove name="UrlRoutingModule"/>
        <add name="UrlRoutingModule" 
             type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </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>

3. Create Routes in your global.asax file

Note: You have to set Application Pool to Asp.net 4.0 application pool , as routing is not working with Asp.net 4.0 Classic Application pool.

Hope this will help.

Suckling answered 23/8, 2010 at 8:39 Comment(3)
Is this really supposed to say UrlRoutingHandler 2.0.0.0? why wouldn't it be 4.0?Abbey
This worked for me, but I did not need to add the UrlRoutingHandler node.Tenerife
I'm running into something similar, but this asnwer did not work, could you have a look: #34080641Lodie
G
1

I've read all your recipes but my web site (ASP.NET 4.0 + VS2010 + Cassini) was still not routing correctly.

The Virtual Path for my site was "CompanyName.ApplicationName.Web". I changed this virtual to "MyApplicationName" and voila!

Change the Cassini's Virtual Path configuration:

  • Cassini's Virtual Path -> Ctrl + W, P or;
  • Right click the Web Site and "Properties Window".
Gabelle answered 7/11, 2011 at 17:13 Comment(1)
So glad I found this answer! I was using a virtual path that had a period "." in it. After changing my virtual path from "mysite.com" to "mysite", all my custom routing worked.Barolet
M
1

My solution, after trying EVERYTHING:

Bad deployment, an old PrecompiledApp.config was hanging around my deploy location, and making everything not work.

My final settings that worked:

  • IIS 7.5, Win2k8r2 x64,
  • Integrated mode application pool
  • Nothing changes in the web.config - this means no special handlers for routing. Here's my snapshot of the sections a lot of other posts reference. I'm using FluorineFX, so I do have that handler added, but I did not need any others:

    <system.web>
      <compilation debug="true" targetFramework="4.0" />
      <authentication mode="None"/>
    
      <pages validateRequest="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
      <httpRuntime requestPathInvalidCharacters=""/>
    
      <httpModules>
        <add name="FluorineGateway" type="FluorineFx.FluorineGateway, FluorineFx"/>
      </httpModules>
    </system.web>
      <system.webServer>
        <!-- Modules for IIS 7.0 Integrated mode -->
        <modules>
          <add name="FluorineGateway" type="FluorineFx.FluorineGateway, FluorineFx" />
        </modules>
    
        <!-- Disable detection of IIS 6.0 / Classic mode ASP.NET configuration -->
        <validation validateIntegratedModeConfiguration="false" />
      </system.webServer>
    
  • Global.ashx: (only method of any note)

    void Application_Start(object sender, EventArgs e) {
        // Register routes...
        System.Web.Routing.Route echoRoute = new System.Web.Routing.Route(
              "{*message}",
            //the default value for the message
              new System.Web.Routing.RouteValueDictionary() { { "message", "" } },
            //any regular expression restrictions (i.e. @"[^\d].{4,}" means "does not start with number, at least 4 chars
              new System.Web.Routing.RouteValueDictionary() { { "message", @"[^\d].{4,}" } },
              new TestRoute.Handlers.PassthroughRouteHandler()
           );
    
        System.Web.Routing.RouteTable.Routes.Add(echoRoute);
    }
    
  • PassthroughRouteHandler.cs - this achieved an automatic conversion from http://andrew.arace.info/stackoverflow to http://andrew.arace.info/#stackoverflow which would then be handled by the default.aspx:

    public class PassthroughRouteHandler : IRouteHandler {
    
        public IHttpHandler GetHttpHandler(RequestContext requestContext) {
            HttpContext.Current.Items["IncomingMessage"] = requestContext.RouteData.Values["message"];
            requestContext.HttpContext.Response.Redirect("#" + HttpContext.Current.Items["IncomingMessage"], true);
            return null;
        }
    }
    
Monocyclic answered 17/3, 2012 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.