ASP.NET Core の静的ファイル

静的ファイル

静的ファイルには、HTML、CSS、画像、JavaScript、その他の静的リソース ファイルが含まれます。
つまり、Web サイトのコンテンツそのものです。

静的ファイルサービス

静的ファイルは、プロジェクトの Web ルート ディレクトリ、wwwroot フォルダに保存されます。
wwwroot ディレクトリはコンテンツ ルートのサブディレクトリであり、他のコード ファイル、.exe および .dll ファイルも含まれています。
wwwroot フォルダーの構造は次のとおりです。

  • css
  • js
  • ライブラリ
  • 画像
    • 1.jpg

UseStaticFiles を呼び出してアプリの起動時に静的ファイル サービスを有効にすると、wwwroot ディレクトリが https:/// にマップされます。

app.UseStaticFiles();

この画像ファイルには、URL https://localhost:5001/images/1.jpg を通じてアクセスできます。

他のディレクトリをマップする

他のディレクトリを https:///otherroot/images/1.jpg にマップする場合は、次のようになります。

  • wwwroot
    • css
    • 画像
    • js
  • その他ルート
    • 画像
      • 1.jpg

次のようにマッピングできます。

app.UseStaticFiles(new StaticFileOptions
{
    
    
    FileProvider = new PhysicalFileProvider(
           Path.Combine(builder.Environment.ContentRootPath, "otherroot")),
    RequestPath = "/otherroot",
    OnPrepareResponse = ctx =>
    {
    
    
        var cacheMaxAgeOneWeek = (60 * 60 * 24 * 7).ToString();
        ctx.Context.Response.Headers.Append(
            "Cache-Control", $"public, max-age={
      
      cacheMaxAgeOneWeek}"); //添加过期时间
    }
});

次に、次のように HTML コードでそれを参照できます。

<img src="~/otherroot/images/1.png" class="img" asp-append-version="true" alt="Test">

静的ファイルのアクセス許可を設定する

3 つの条件を満たす必要があります。

  • wwwroot の下に保存されていません
  • 最初に UseAuthorization を呼び出し、次に UseStaticFiles を呼び出します
  • フォールバック認可ポリシーを設定する
builder.Services.AddAuthorization(options =>
{
    
    
    options.FallbackPolicy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
});

これらのファイルへの匿名アクセスを禁止します。

ファイルディレクトリをリストする

ディレクトリ ブラウズ サービスは、ディレクトリ内のすべてのファイルを一覧表示できます。
AddDirectoryBrowser() を介して有効にできます。

builder.Services.AddDirectoryBrowser();
var fileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.WebRootPath, "MyImages"));
var requestPath = "/MyImages";
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
    
    
    FileProvider = fileProvider,
    RequestPath = requestPath
});

デフォルトのファイルを使用する

app.UseDefaultFiles() を使用した後、

app.UseDefaultFiles();

これらのファイルは、エントリとして wwwroot の下で順番に検索されます。

  • デフォルト.htm
  • デフォルト.html
  • インデックス.htm
  • インデックス.html

ファイル名 mydefault.html を指定することもできます。

var options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("mydefault.html");
app.UseDefaultFiles(options);

あなたはファイルサーバー上にいます

app.UseFileServer は、UseStaticFiles、UseDefaultFiles、または UseDirectoryBrowser の組み合わせです。

builder.Services.AddDirectoryBrowser();
app.UseFileServer(enableDirectoryBrowsing: true);

上記のコードにより、UseStaticFiles、UseDefaultFiles、および DirectoryBrowsing が有効になります。

FileExtensionContentTypeProvider

FileExtensionContentTypeProvider の Mappings プロパティは、ファイル拡張子を MIME コンテンツ タイプにマップできます。

// Set up custom content types - associating file extension to MIME type
var provider = new FileExtensionContentTypeProvider();
// Add new mappings
provider.Mappings[".myapp"] = "application/x-msdownload";
provider.Mappings[".htm3"] = "text/html";
provider.Mappings[".image"] = "image/png";
// Replace an existing mapping
provider.Mappings[".rtf"] = "application/x-msdownload";
// Remove MP4 videos.
provider.Mappings.Remove(".mp4");
app.UseStaticFiles(new StaticFileOptions
{
    
    
    ContentTypeProvider = provider
});

おすすめ

転載: blog.csdn.net/cuit/article/details/132577692