6

だから私は Owin と Katana で遊んでいて、パブリック フォルダーに静的ファイルを提供したいと考えています。

スタイルシートを含む Content フォルダーとスクリプト フォルダーがあります。

私のスタートアップ:

    public void Configuration(IAppBuilder app)
    {
 #if DEBUG
        //when things go south
        app.UseErrorPage();
  #endif

        // Remap '/' to '.\public\'.
        // Turns on static files and public files.
        app.UseFileServer(new FileServerOptions()
        {
            RequestPath = PathString.Empty,
            FileSystem = new PhysicalFileSystem(@".\public"),
        });

    }

したがって、localhost:8861/ を参照すると、パブリック フォルダー内の index.html ファイルに移動します。それで大丈夫です。しかし、ブロックしたい localhost:8861/Content/style.css を参照することもできます。ユーザーが必要とするすべてのものは、パブリック フォルダーでアクセスできる必要があります。残りはすべてブロックする必要があります。

どうすればこれを達成できますか?

4

2 に答える 2

3

最小限のファイル処理が必要な場合は、どのファイルを提供するか、または提供しないかを完全に制御し、ミドルウェアを使用して完全に制御できます。開発中にキャッシュされていないファイル サービングが必要だったので、これを行いました。

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Web;

namespace Owin
{
    using AppFunc = Func<IDictionary<string, object>, Task>;

    public static class DynamicFileExtension
    {    
        /// <summary>
        /// ONLY use during development
        /// </summary>
        public static void UseDynamicFiles(this IAppBuilder app, string baseDirectory)
        {
            app.Use(new Func<AppFunc, AppFunc>(next => (async context =>
            {
                var method = (string) context["owin.RequestMethod"];
                var requestpath = (string) context["owin.RequestPath"];
                var scheme = (string) context["owin.RequestScheme"];
                var response = (Stream) context["owin.ResponseBody"];
                var responseHeader = (Dictionary<string, string[]>) context["owin.ResponseHeaders"];

                if (method == "GET" && scheme == "http")
                {
                    var fullpath = baseDirectory + requestpath;

                    // block logic...     

                    if (File.Exists(fullpath))
                    {

                        using (var file = File.OpenRead(fullpath))
                        {
                            await file.CopyToAsync(response);
                        }

                        var mime = MimeMapping.GetMimeMapping(fullpath);

                        responseHeader.Add("Content-Type", new[] {mime});

                        return;
                    }
                }

                await next.Invoke(context);
            })));
        }
    }
} 

本番環境では使用しませんが、うまくいきました。

于 2016-03-04T22:46:24.753 に答える