我正在編寫一個CMS系統,在閱讀並通過幾個例子工作之後,我已經解決了HttpHandlerFactory以執行我所需要的操作。使用HttpHandlerFactory來渲染CMS和物理頁面
關鍵是我們的網站通常是複製和註冊過程的混合。所以我現在需要使用默認的HttpHandler for aspx來渲染物理註冊頁面,直到我可以用內容管理它們的方式。
創建的處理程序類後添加以下到我的網站的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;
}
您的權利,它不是一個完整的答案,但已經驗證了我正在嘗試的東西。 – 2010-10-05 15:38:09