私はCMSシステムを作成している最中であり、いくつかの例を読んで作業した後、必要なことを実行するためにHttpHandlerFactoryに落ち着きました。
重要な点は、私たちのサイトは一般的にコピーと登録のプロセスが混在しているということです。そのため、現在、aspxのデフォルトのHttpHandlerを使用して、物理的な登録ページをレンダリングする必要があります。これにより、コンテンツ管理もできるようになります。
ハンドラークラスを作成した後、WebサイトのWeb構成に以下を追加しました
<add verb="*" path="*.aspx" type="Web.Helpers.HttpCMSHandlerFactory, Web.Helpers"/>
上記のパスは物理ページとcms駆動ページを処理するため、コードを少しチェックするだけで、ページが物理的に存在するかどうかを確認し、目的のページをレンダリングできます。
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
string pageName = Path.GetFileNameWithoutExtension(context.Request.PhysicalPath);
context.Items.Add("PageName", pageName);
//DirectoryInfo di = new DirectoryInfo(context.Request.MapPath(context.Request.ApplicationPath));
FileInfo fi = new FileInfo(context.Request.MapPath(context.Request.CurrentExecutionFilePath));
//var file = fi.Where(x => string.Equals(x.Name, string.Concat(pageName, ".aspx"), StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
if (fi.Exists == false)
{
// think I had this the wrong way around, the url should come first with the renderer page second
return PageParser.GetCompiledPageInstance(url, context.Server.MapPath("~/CMSPage.aspx"), context);
}
else
{
return PageParser.GetCompiledPageInstance(context.Request.CurrentExecutionFilePath, fi.FullName, context);
}
}
PageParser.GetCompiledPageInstance
私が持っている質問は、物理的なページがあるとき以外のものを使用する必要があるかどうかです。
更新:上記以降、画像用のHttpHandlerの開発を続けました。これも、画像が存在する場合はデータベースから提供されるものを使用するのと同じ原則で機能します。pngファイルに少し問題がありましたが、以下のプロセスは表示されているファイル形式で機能します。
byte[] image = null;
if (File.Exists(context.Request.PhysicalPath))
{
FileStream fs = new FileStream(context.Request.PhysicalPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
image = br.ReadBytes((int)fs.Length);
}
else
{
IKernel kernel = new StandardKernel(new ServiceModule());
var cmsImageService = kernel.Get<IContentManagementService>();
var framework = FrameworkSetup.GetSetFrameworkSettings();
image = cmsImageService.GetImage(Path.GetFileName(context.Request.PhysicalPath), framework.EventId);
}
var contextType = "image/jpg";
var format = ImageFormat.Jpeg;
switch (Path.GetExtension(context.Request.PhysicalPath).ToLower())
{
case ".gif":
contextType = "image/gif";
format = ImageFormat.Gif;
goto default;
case ".jpeg":
case ".jpg":
contextType = "image/jpeg";
format = ImageFormat.Jpeg;
goto default;
case ".png":
contextType = "image/png";
format = ImageFormat.Png;
goto default;
default:
context.Cache.Insert(context.Request.PhysicalPath, image);
context.Response.ContentType = contextType;
context.Response.BinaryWrite(image);
context.Response.Flush();
break;
}