2

我遇到了一些问题,我在 web.config 文件中创建了一个类型,用作 aspx 的 HTTPHandler,代码..

<add path="*.aspx" verb="*" type="myHandler"></add>

cs页面是

public class myHandler :  IHttpHandler
{
    public void ProcessRequest(System.Web.HttpContext context) 
    {
        // check if client browser can accept gzip encoding
        string AcceptEncoding = context.Request.Headers["Accept-Encoding"];
        if (!string.IsNullOrEmpty(AcceptEncoding) && AcceptEncoding.Contains("gzip"))
        {
            // if yes, apply it
            context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
            context.Response.AddHeader("Content-Encoding", "gzip");
        }
        // don't do anything else, just let the default page handling work
    }

    public bool IsReusable
    {
        get { return false; }
    }

}

如您所见,我正在检查客户端是否接受 gzip 编码,如果是,请添加它,并让默认进程处理...... 我得到的响应是......

XML Parsing Error: no element found
Location: http://localhost:49903/Masters/Default.aspx
Line Number 1, Column 1:
^

每个页面都返回此错误?我猜想在我的处理程序中处理了请求后,它会以某种方式清除其他所有内容或相关内容。

萤火虫显示以下..

Response Headers

Cache-Control   private
Connection  Close
Content-Encoding    gzip
Content-Length  0
Date    Sat, 06 Oct 2012 13:14:50 GMT
Server  ASP.NET Development Server/10.0.0.0
X-AspNet-Version    4.0.30319


Request Headers

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Cookie  ASP.NET_SessionId=1wfoaqta3mosntii1c3ual1i; .ASPXAUTH=D41BBDC2CCF15048BB5A345EBBFBC63EAE35FD37E2F1F170C1D68495477889A989353D4EB30F2B9A723F83C21293A47478B654A1BD2453BCF032DC539427EA0E519D2FEE70DB7660F00AA90E159633C4
Host    localhost:49903
Referer http://localhost:49903/Masters/Default.aspx
User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0.1


Request Headers From Upload Stream

Content-Length  8241
Content-Type    multipart/form-data; boundary=---------------------------2995119424827

所以,从我的猜测来看,我想知道有没有一种方法可以在我按照我的方式处理页面之后,我可以做类似base.ProcessRequest(). 我知道这是不可能的,因为我正在实现一个接口而不是从一个类派生,但我想知道,在完成我的页面后,如何让 asp.net 引擎处理页面?

注意这个问题不是关于实现 gzib 编码,我知道我可以在 web 配置中设置它,在每个页面上,在 IIS 中,在实用程序类中创建一个静态函数并调用它,以及许多其他的东西。但同样,请不要回答我可以使用不同的方式来实现这个 gzip 编码,我知道我可以,我关心的是如何让 asp.net 做它的默认处理(或任何可能的意思)之后我完成了我的自定义处理?

4

1 回答 1

1

你走错了路。您尝试使用的处理程序是为处理以不同扩展名结尾的某种文件提供一种完全不同的方式,方法是添加.aspx然后您处理该页面并且根本不允许 asp.net 处理这些页面。

你应该做的是制作一个httpModule,例如一个像这样的类:

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

    void IHttpModule.Dispose()
    {
    }

    private void context_BeginRequest(object sender, EventArgs e)
    {
        // gzip here
    }
}

在网络配置上:

<httpModules>
   <add type="gZipModule"  name="gZipModule" />
</httpModules>

或者更简单,在gZip上global.asax制作Application_BeginRequest

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    // check if the file is .aspx, then make gzip
    string cTheFile = HttpContext.Current.Request.Path;
    string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);

    if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
    {   
        // now make gZip
    }
}
于 2012-10-06T15:03:49.207 回答