拦截* .aspx

时间:2010-12-01 16:16:40

标签: asp.net httpwebrequest

我正在尝试拦截每个aspx请求。拦截有效,但页面保持空白。我错过了什么?

namespace WebSite
{
    public class Class1 : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {

        }
    }
}

<system.webServer>
    <handlers>
      <add name="SampleHandler" verb="*"
        path="*.aspx"
        type="WebSite.Class1, WebSite"
        resourceType="Unspecified" />
    </handlers>
  </system.webServer>

3 个答案:

答案 0 :(得分:2)

您正在拦截页面请求,然后您没有对其执行任何操作。如果你希望看到某种输出,你必须对传入的HttpContext执行某种操作。下面是一些文章,在处理HttpContext时可能是不错的阅读。简而言之,如果您希望看到响应,则必须为其生成一些内容。

http://odetocode.com/Articles/112.aspx
What is the difference between HttpContext.Current.Response and Page.Response?
http://www.c-sharpcorner.com/uploadfile/desaijm/asp.netposturl11282005005516am/asp.netposturl.aspx

答案 1 :(得分:0)

你并没有真正拦截他们。这更像是劫持他们。每个* .aspx请求都将转到此处理程序而不是实际的* .aspx页面。更合适的方法是让您查看Application_BeginRequest中的global.asax处理程序。

答案 2 :(得分:0)

我使用IhttpHandler接口来处理我的图像返回。

IHttpHandlerFactory是我用来处理页面拦截的东西:

public class HttpCMSHandlerFactory : IHttpHandlerFactory
{
    // collects page name requested
    string pageName = Path.GetFileNameWithoutExtension(context.Request.PhysicalPath);
    // Add the page name to the context
    context.Items.Add("PageName", pageName);
    // I can still check if the page physically exists else pass on to my CMS handler: CMSPage.aspx
    FileInfo fi = new FileInfo(context.Request.MapPath(context.Request.CurrentExecutionFilePath));
    if (fi.Exists == false)
    {
        // if page doesnt exist context info is passed on to CMSPage to handle copy
        return PageParser.GetCompiledPageInstance(string.Concat(context.Request.ApplicationPath, "/CMSPage.aspx"), url, context);
    }
    else
    {
        // if page exist physical page is returned
        return PageParser.GetCompiledPageInstance(context.Request.CurrentExecutionFilePath, fi.FullName, context);
    }
}

check out my previous post on the subject

相关问题