views:

871

answers:

3

I'm using the new routing functionality in ASP.NET 3.5 to act as my catch-all for page requests to my website. I've registered my route as follows within the global.asax,

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {

        RegisterRoutes(RouteTable.Routes);

    }

    private void RegisterRoutes(RouteCollection Routes)
    {

        Route r = new Route("{*URL}", new MyRouteHandler());
        Routes.Add(r);

    }

</script>

The code works absolutely fine for all URLs except / (the root page). If I go to any other URL /blah/something/foo/ it works fine and my handler is run as expected.

How can I get it to run over the root page? I am running the code via Visual Studio 2008's build in web server.

+1  A: 

Cassini (Visual Studio's built-in webserver) may have a problem with routing.

To quote from Dmitryr's blog:

Cassini had a problem with ASP.NET projects using Routing due to the directory listing feature. Cassini would respond with the directory listing to any request for a directory, without handing over the request to ASP.NET. As a result, requests for friendly URLs would get intercepted by Cassini and never reach ASP.NET.

This update (v3.5.0.1) changes the logic around – all requests go to ASP.NET first, then Cassini intercepts 404 responses and, if the request was for a directory, it responds with the directory listing. Applications relying on friendly URLs / Routing now work better with Cassini.

You've not mentioned what behavior you see when you go to the root. If you're not getting a directory listing, perhaps you need to remove your default page?

Robin M
I took a look at this but couldn't seem to find a compiled version of the app or instructions on where to actually install it after compiling.I've found a solution which I'm posting above.
Craig Bovis
A: 

I'm not sure that you can target the root with web.routing.

I'm making the big assumption here that you have implemented web.routing on a web forms application. If that is the case, you are still capturing your root requests to your default page (i.e. Default.aspx), in which case can you deal with the request from there.

My web.routing implementations in the past have targeted an ASPX page in the root directory which I have also set up as the default page. If a request hits that page without any routing information, I assume the request is for home.

If, however, you are using the web forms engine, but using a MVC style folder structure. I'd suggest either adding default pages that redirect appropriately or possibly capturing the request in the BeginRequest event.

Rich

kim3er
A: 

@Craig Bovis : Can you post some example snippet or more details on how to handle it?, I am facing exactly similar problem.

Vijay Rayapati