3

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

4

1 回答 1

0

如果我正确理解您的问题,我相信您的路径应该是这样的:

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

path=* 将使它捕获所有请求,而 *.aspx 将它限制为带有 .aspx 扩展名的请求。

于 2012-10-08T12:13:59.730 回答