3
votes

I'm using VS 2010, .NET 4.0, ASP.NET Web Application project.

I have custom HttpHandler for handling *.aspx. In root folder I have Default.aspx page that is used just to catch root folder access and to redirect request to other .aspx page, and that request is further processed by my custom handler.

If request is directed to root folder, Default.aspx will be processed. If request is sent to *.aspx (any other then Default.aspx) my custom handler will process it.

I want to remove Default.aspx page from project, and to still be able to catch request to my root application folder, and redirect request to my custom handler.

I have tried with registering a route in Global.asax:

private void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Default",
            "",
            "~/Default.aspx");
    }

that didn't work.

Also, I have tried registering in web.config handler:

<add name="DefaultHandler" path="*" verb="*" type="App.DefaultHandler, App" />

but that didn't work too.

UPDATE:

Actually, handler defined in web.config path="*" does work, but it catches all requests to server which I don't think is good practice.

I have created DefaultModule, where on BeginRequest event I check Path property in Request and assume that is request to default page. So far, so good, but I'm not sure it will work in all cases, here is code:

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpRequest request = HttpContext.Current.Request;
        if (request.Path == "/")
        {
            HttpContext.Current.RewritePath("custom.aspx");
        }
    }

If anyone has any better idea I'd appreciate it, thanks.

UPDATE:

I have tried using this solution: https://stackoverflow.com/a/1913073/84852 I thought that web server will request Default.aspx and then I could catch that request using handler described, but application just throughs "File not found" exception.

Anyway, I like solution with HttpModule, and if there is no other solution presented I will go with it. I'm just worried about condition if (request.Path == "/"), so I've changed it to

if (context.Request.CurrentExecutionFilePath == context.Request.ApplicationPath)

just in case.

New ideas are still welcome. Thanks.

UPDATE:

Solution with HttpModule doesn't work on IIS, but only on VS development server. Problem is that Session is not started when request gets to my custom HttpHandler (which btw implements IRequiresSessionState).

1
" Default.aspx page that is used just to catch root folder access and to redirect request to other .aspx page" for this bit, you can change the "Default Document" setting in IIS (or web.config) from Default.aspx to (custom.aspx in your case) whatever page you want see here: iis.net/configreference/system.webserver/defaultdocumentrobasta
Default page changes depending on settings in database, it comes from application logic so it cannot be just hardcoded.Andrija
I wrote custom.aspx just as an example. Sorry for confusion :)Andrija

1 Answers

0
votes

If I understand your problem correctly, I believe your path should be this:

<add name="DefaultHandler" path="*.aspx" verb="*" type="App.DefaultHandler, App" />

path=* will make it catch all requests while *.aspx will limit it to request with a .aspx extension.