Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 448
Pragnesh Patel Avatar asked Aug 19 '10 15:08

Pragnesh Patel


3 Answers

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.

like image 144
Pragnesh Patel Avatar answered Oct 14 '22 13:10

Pragnesh Patel


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".
like image 41
Fergara Avatar answered Oct 14 '22 13:10

Fergara


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;
        }
    }
    
like image 31
Andrew Arace Avatar answered Oct 14 '22 13:10

Andrew Arace