根文件夹的asp.net处理程序 [英] asp.net handler for root folder

查看:62
本文介绍了根文件夹的asp.net处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2010,.NET 4.0,ASP.NET Web应用程序项目.

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

我有用于处理* .aspx的自定义HttpHandler.在根文件夹中,我具有Default.aspx页,该页仅用于捕获根文件夹访问并将请求重定向到其他.aspx页,并且该请求由我的自定义处理程序进一步处理.

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.

如果将请求定向到根文件夹,则将处理Default.aspx.如果将请求发送到* .aspx(Default.aspx以外的任何其他设备),我的自定义处理程序将对其进行处理.

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.

我想从项目中删除Default.aspx页面,并且仍然能够将请求捕获到我的根应用程序文件夹,并将请求重定向到我的自定义处理程序.

我尝试在Global.asax中注册路由:

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

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

那没用.

此外,我尝试在web.config处理程序中注册:

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

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

但是那也不起作用.

更新:

实际上,在web.config path ="*" 中定义的处理程序确实可以工作,但是它捕获了对服务器的所有请求,我认为这不是一个好习惯.

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

我创建了DefaultModule,在BeginRequest事件中,我在Request中检查Path属性,并假定这是对默认页面的请求.到目前为止,还算不错,但是我不确定在所有情况下都可以使用,这里是代码:

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.

更新:

我尝试使用此解决方案: https://stackoverflow.com/a/1913073/84852 我以为Web服务器将请求Default.aspx,然后我可以使用所描述的处理程序来捕获该请求,但是应用程序只是通过找不到文件"异常.

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.

无论如何,我喜欢HttpModule的解决方案,如果没有其他解决方案,我将继续使用它.我只是担心条件 if(request.Path =="/"),所以我将其更改为

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)

以防万一.

仍然欢迎新想法.谢谢.

New ideas are still welcome. Thanks.

更新:

使用HttpModule的解决方案在IIS上不起作用,而仅在VS开发服务器上起作用.问题在于,当请求到达我的自定义HttpHandler(顺便说一句实现IRequiresSessionState)时,会话不会启动.

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

推荐答案

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

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

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

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

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

这篇关于根文件夹的asp.net处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆