5

我有一个 ASP.Net 应用程序,通过使用分析器我注意到在我的页面运行之前发生了大量的处理。在我的应用程序中,我们没有使用 viewstate、asp.Net 会话,而且我们可能不需要使用 asp.net 页面生命周期所带来的大部分开销。是否有其他一些我可以轻松继承的类将删除所有 Asp.Net 内容,让我自己处理写入页面?

我听说 ASP.Net MVC 可以大大减少页面加载,仅仅是因为它不使用旧的 asp.net 生命周期,并且以不同的方式处理页面。有没有一种简单的方法,也许只是让我的网页继承一些其他类来利用这样的东西。如果可能的话,我想要一个适用于 ASP.Net 2.0 的解决方案。

4

3 回答 3

9

我发现的大多数文章都在谈论使用 Page 作为基类并在此基础上实现功能,看起来您需要创建自己的 MyPage 类来实现 IHttpHandler

来自 MSDN 文章


using System.Web;

namespace HandlerExample { public class MyHttpHandler : IHttpHandler { // Override the ProcessRequest method. public void ProcessRequest(HttpContext context) { context.Response.Write("This is an HttpHandler Test.");
context.Response.Write("Your Browser:"); context.Response.Write("Type: " + context.Request.Browser.Type + ""); context.Response.Write("Version: " + context.Request.Browser.Version); }

  // Override the IsReusable property.
  public bool IsReusable
  {
     get { return true; }
  }

} }

同样,来自文章:要使用此处理程序,请在 Web.config 文件中包含以下行。


<configuration>
   <system.web>
      <httpHandlers>
         <add verb="*" path="handler.aspx" type="HandlerExample.MyHttpHandler,HandlerTest"/>
      </httpHandlers>
   </system.web>
</configuration>

我会看一下 System.web.ui.page 的源代码,看看它对您的指导作用。我的猜测是,它主要只是以正确的顺序调用 asp.net 页面生命周期中的不同方法。您可以通过从 ProcessRequest 方法调用您自己的 page_load 来做类似的事情。这将路由到您实现 MyPage 的类的个人实现。

我以前从未想过做这样的事情,这听起来不错,因为我真的不使用任何臃肿的网络表单功能。MVC 可能会使整个练习变得徒劳,但它看起来确实很简洁。

我的快速示例

新基地:


using System.Web;
namespace HandlerExample
{
    // Replacement System.Web.UI.Page class
    public abstract class MyHttpHandler : IHttpHandler
    {
        // Override the ProcessRequest method.
        public void ProcessRequest(HttpContext context)
        {
            // Call any lifecycle methods that you feel like
            this.MyPageStart(context);
            this.MyPageEnd(context);
        }

    // Override the IsReusable property.
    public bool IsReusable
    {
        get { return true; }
    }

    // define any lifecycle methods that you feel like
    public abstract void MyPageStart(HttpContext context);
    public abstract void MyPageEnd(HttpContext context);

}

页面实现:

// Individual implementation, akin to Form1 / Page1
public class MyIndividualAspPage : MyHttpHandler
{

    public override void MyPageStart(HttpContext context)
    {
        // stuff here, gets called auto-magically
    }

    public override void MyPageEnd(HttpContext context)
    {
        // stuff here, gets called auto-magically
    }
}

}

于 2009-02-25T16:26:24.170 回答
3

如果您不需要所有这些“asp.net 东西”,您可能希望实现一个自定义 IHttpHandler。Afaik,除了 Page 类之外,没有其他标准的 IHttpHandler 可以重用。

于 2009-02-25T15:34:57.803 回答
0

为此,您应该首先查看 System.Web.UI.PageHandlerFactory 类和相应的 System.Web.IHttpHandlerFactory 接口。

从那里您可能会看到 System.Web.IHttpHandler 接口和 System.Web.UI.Page 类。

基本上,您将编写自己的 IHttpHandlerFactory 来生成处理页面请求的 IHttpHandler。

于 2009-02-25T17:41:46.577 回答