代码之家  ›  专栏  ›  技术社区  ›  Luke Duddridge

使用HttpHandlerFactory呈现CMS和物理页面

  •  0
  • Luke Duddridge  · 技术社区  · 16 年前

    我正在写一个CMS系统,在阅读和处理了几个例子之后,我已经确定了 HttpHandlerFactory 去完成我需要的。

    关键是我们的网站通常是复制和注册的混合过程。因此,我目前需要使用默认的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

            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 回复  |  直到 15 年前
        1
  •  1
  •   Adrian K    15 年前

    我不确定这是否完全回答了你的问题。。。我还建立了一个ASP.NETCMS是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>
    
    推荐文章