2010-09-09 40 views
0

我正在編寫一個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; 
     } 

回答

1

我不知道這完全回答你的問題......我還建立了一個ASP.NET的CMS是HttpHandler的驅動,並且還允許物理.aspx頁。由於我只有少量物理.aspx文件和位置,因此管理執行的最簡單方法是通過web.config。

首先,我配置的網站(一般條款)用我的處理程序 - 除了登錄頁面(作爲一個例子):

<add verb="*" path="login.aspx" type="System.Web.UI.PageHandlerFactory"/> 
<add verb="*" path="Register.aspx" type="System.Web.UI.PageHandlerFactory"/> 
<add verb="*" path="*.aspx" type="Morphfolia.PublishingSystem.HttpHandlers.DefaultHandler, Morphfolia.PublishingSystem"/> 

你可以做的另一件事是由location隔離,所以對於我選擇使用出的現成的ASP.NET處理程序通常處理「經典」 ASP.NET請求網站的這一部分:

<location path="Morphfolia/_publishing"> 
    <system.web> 
    <httpHandlers> 
     <add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/> 
    </httpHandlers> 
    </system.web> 
</location> 
+0

您的權利,它不是一個完整的答案,但已經驗證了我正在嘗試的東西。 – 2010-10-05 15:38:09