替代System.web.ui.page的选项

5
我有一个ASP.Net应用程序,通过使用分析工具,我注意到在我的页面运行之前会有相当多的处理。在我的应用程序中,我们不使用viewstate、asp.Net session,而且我们可能不需要使用asp.net页面生命周期所带来的大部分开销。是否有其他类可以轻松继承,削减所有Asp.Net内容,并让我自己处理写入页面的操作?
我听说ASP.Net MVC可以显著缩短页面加载时间,仅因为它不使用旧的asp.net生命周期,并以不同的方式处理页面。是否有一种简单的方法,例如通过让我的网页继承其他类来利用这样的东西。如果可能的话,我希望这个解决方案适用于ASP.Net 2.0。
3个回答

10

我发现大部分文章都在讲使用页面作为基类,并在其上实现功能,看起来你需要创建自己的MyPage类并实现IHttpHandler接口

From the MSDN article


using System.Web;

命名空间 HandlerExample { public class MyHttpHandler : IHttpHandler { // 重写 ProcessRequest 方法。 public void ProcessRequest(HttpContext context) { context.Response.Write("这是一个 HttpHandler 测试。
"); context.Response.Write("您的浏览器:"); context.Response.Write("类型:" + context.Request.Browser.Type + "
"); context.Response.Write("版本:" + context.Request.Browser.Version); } }

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

以下是需要翻译的内容:

再次引用文章内容:要使用此处理程序,请在 Web.config 文件中包含以下行。

要使用此处理程序,需在 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 可能会使整个练习徒劳无功,但它看起来相当不错。

我的快速示例

new base:


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);

}

Implementation for the page:

// 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
    }
}

}


3

如果您不需要所有这些“asp.net的东西”,您可以考虑实现自定义的IHttpHandler。据我所知,除了Page类之外,没有其他标准的IHttpHandlers可供重用。


0

要做到这一点,您应该首先查看System.Web.UI.PageHandlerFactory类和相应的System.Web.IHttpHandlerFactory接口。

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

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


这一步是可选的吗?MSDN上关于此的所有内容都是针对创建处理程序以支持自定义扩展名(如*.sample)而非*.aspx。只是好奇,谢谢。 - Allen Rice
在最简单的情况下,您不需要IHttpHandlerFactory,因为您可以注册一个处理文件类型的所有请求的单个IHttpHandler。通过实现IHttpHandlerFactory,您可以查看请求并根据需要返回不同的实现。 - Rune Grimstad
这就是ASP.Net使用PageHandlerFactory实现的。根据请求的url,它将返回一个在具有相同名称的Page类中实现的IHttpHandler。因此,您不必拥有一个处理文件类型所有请求的IHttpHandler,而是可以拥有多个。 - Rune Grimstad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接