ページを基本クラスとして使用し、その上に機能を実装することについて話している記事のほとんどは、IHttpHandler を実装する独自の MyPage クラスを作成する必要があるようです。
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 を実装するクラスの個々の実装にルーティングされます。
私はこれまでこのようなことを考えたことはありませんでした。肥大化した Web フォーム機能をまったく使用していないので、かなり良さそうです。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
}
}
}