ASP.NET Core アプリケーションで特定のディレクトリ、URLのみ静的ファイルにルーティングするコードを紹介します。
概要
ASP.NET Core Webアプリケーションで
app.UseStaticFiles()
メソッドを呼び出すと wwwroot フォルダ内の静的ファイルをルーティングできます。
この処理により、wwwroot フォルダ内のすべてのファイルがルーティングされます。
通常のアプリケーションではこの処理で問題ありませんが、余計なフォルダの静的ファイルをルーティングしたくない場合があり、指定したフォルダやURLの場合にのみ
静的ファイルへルーティングしたいことがあります。
この記事では、指定したURLやフォルダのみ、静的ファイルにルーティングするコードを紹介します。
方針
app.UseStaticFiles()
メソッドの引数に
StaticFileOptions
を与えることで、指定したURLに対してルーティングを実行できます。
プログラム例
コード
静的ファイルをソリューションエクスプローラーの
/files
ディレクトリに配置します。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<p>テストページです。</p>
</body>
</html>
RazorPage を
/Pages
ディレクトリに配置します。
@page ディレクティブで "/app/exec" が指定されているため、
(アプリケーションルートURL)/app/exec
のURLでこのページが表示されます。
@page "/app/exec"
@model SpecificUrlStaticFileRouting.Pages.IndexModel
@{
}
<html>
<head>
</head>
<body>
<p>Razor Pageのテストページです。</p>
</body>
</html>
Startup.cs ファイルを編集します。
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;
using System.IO;
namespace SpecificUrlStaticFileRouting
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
StaticFileOptions opt = new StaticFileOptions();
opt.FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "files"));
opt.RequestPath = "/app/contents";
app.UseStaticFiles(opt);
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
ファイル配置
ソリューションエクスプローラーのファイル配置は下図の通りです。
解説
UseRouting()
メソッドを呼び出しルーティングの機能を利用します。
app.UseRouting();
StaticFileOptions
オブジェクトを作成します。
StaticFileOptions opt = new StaticFileOptions();
FileProvider プロパティに PhysicalFileProvider オブジェクトのインスタンスを設定します。PhysicalFileProvider オブジェクトのコンストラクタに、
静的ファイルを配置したディレクトリパスを与えます。
アプリケーションのファイルを配置パス(アプリケーションルートの物理パス)は、
env.ContentRootPath
で取得できるため、
アプリケーションの配置パスと静的ファイルを配置したフォルダを
Path.Combine
メソッドで結合して静的ファイルを配置したパスを与えます。
opt.FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "files"));
RequestPath プロパティにルーティングするURLパスを指定します。
下記のコードでは "/app/contents" を設定しているため、
(アプリケーションルートURL)/app/contents
のURLが
"files" フォルダの静的ファイルにルーティングされる動作となります。
opt.RequestPath = "/app/contents";
作成した、StaticFileOptions オブジェクトを
UseStaticFiles()
メソッドの引数に与えます。
app.UseStaticFiles(opt);
UseEndpoints メソッドではRazorPagesへのルーティング処理を実行します。
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
実行結果
上記のプロジェクトを実行します。
プロジェクト起動後、Webブラウザで
(アプリケーションルートURL)/app/exec
URLにアクセスします。
下図のRazor Pageの画面が表示されます。
続いて、Webブラウザで
(アプリケーションルートURL)/app/contents/index.html
URLにアクセスします。
下図の静的ファイルのページが表示されます。filesフォルダに配置した
index.html
のページが表示されることが確認できます。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2021-05-10
作成日: 2021-05-10