代码之家  ›  专栏  ›  技术社区  ›  Kibbee

system.web.ui.page的备选方案

  •  5
  • Kibbee  · 技术社区  · 15 年前

    我有一个ASP.NET应用程序,通过使用profiler,我注意到在我的页面运行之前有大量的处理过程。在我的应用程序中,我们不使用viewstate、ASP.NET会话,而且我们可能不需要使用ASP.NET页生命周期所带来的大部分开销。有没有其他类我可以很容易地从中继承,将删除所有的ASP.NET东西,并让我自己处理写入页面?

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

    3 回复  |  直到 12 年前
        1
  •  9
  •   Allen Rice 0x6A75616E    12 年前

    我发现的大多数文章都在讨论如何将页面用作基类并在此基础上实现功能,看起来您需要使自己的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方法调用您自己的页面加载,您可以做类似的事情。这将路由到实现mypage的类的单个实现。

    我以前从来没有想过要做这样的事情,这听起来很不错,因为我真的不使用任何膨胀的webforms功能。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
        }
    }
    

    }

        2
  •  3
  •   amartynov    15 年前

    如果您不需要所有这些“ASP.NET东西”,您可能希望实现一个自定义的IHttphandler。在afaik中,除了page类之外,没有其他标准的IHttphandler可以重用。

        3
  •  0
  •   Rune Grimstad    15 年前

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

    从这里您可能会看到System.Web.IHttphandler Inferface和System.Web.UI.Page类。

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

    推荐文章
    nat  ·  主页面破坏pre_init
    12 年前