我发现的大多数文章都在谈论使用 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
}
}
}