0

私は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;
        }
4

1 に答える 1

1

これがあなたの質問に完全に答えるかどうかはわかりません...私はまた、HttpHandler駆動で物理的な.aspxページを可能にするASP.NETCMSを構築しました。物理的な.aspxファイルと場所が少ないため、実行を管理する最も簡単な方法はweb.configを使用することでした。

まず、(例として)ログインページを除いて、ハンドラーを使用するようにWebサイトを(一般的に)構成します。

<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>
于 2010-10-04T21:37:40.573 に答える