加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – System.web.ui.page的替代品

发布时间:2020-12-16 03:20:52 所属栏目:asp.Net 来源:网络整理
导读:我有一个ASP.Net应用程序,我注意到通过使用分析器,在我的页面运行之前发生了大量的处理.在我的应用程序中,我们没有使用viewstate,asp.Net会话,我们可能不需要使用asp.net页面生命周期所带来的大部分开销.还有其他一些我可以轻易继承的课程,它会删除所有的Asp
我有一个ASP.Net应用程序,我注意到通过使用分析器,在我的页面运行之前发生了大量的处理.在我的应用程序中,我们没有使用viewstate,asp.Net会话,我们可能不需要使用asp.net页面生命周期所带来的大部分开销.还有其他一些我可以轻易继承的课程,它会删除所有的Asp.Net内容,让我自己处理这个页面吗?

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

解决方法

我发现大多数文章都在谈论使用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的类的单独实现.

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

}

// Override the IsReusable property. public bool IsReusable { get { return true; } }<configuration> <system.web> <httpHandlers> <add verb="*" path="handler.aspx" type="HandlerExample.MyHttpHandler,HandlerTest"/> </httpHandlers> </system.web> </configuration>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,gets called auto-magically
    }
}

}

// 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,gets called auto-magically
    }
}

}

// Individual implementation,gets called auto-magically } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读