ASP.NET Core Webアプリケーションの静的ファイルのフォルダ(wwwrootフォルダ)のデフォルトドキュメントを設定するコードを紹介します。
概要
ASP.NET Core Webアプリケーションではwwwrootフォルダを作成し、
UseStaticFiles()
メソッドを呼び出すと、wwwrootフォルダを
静的ファイルのディレクトリとして動作させることができます。
デフォルトの状態では静的ファイルのディレクトリにはデフォルトドキュメントが設定されていません。この記事では、ASP.NET Coreアプリケーションの
静的ファイルへのルーティングで、デフォルトドキュメントを設定する手順を紹介します。
UseStaticFiles() のみを呼び出した場合の動作
ASP.NET Core Webアプリケーションで、UseStaticFiles() のみを呼び出した場合の動作を確認します。
ファイル作成
ASP.NET Core Webアプリケーションを作成し、プロジェクトのルートに wwwroot フォルダを作成します。
フォルダ内にsubdir フォルダも作成し、index.html, default.html, start.html のファイルを作成します。
ソリューションエクスプローラーのファイル配置は下図の通りです。
コード
ASP.NET Core Webアプリケーションを作成し、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;
namespace StaticFileDefaultDocument
{
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)
{
}
// 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.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}
解説
app.UseStaticFiles() メソッドを呼び出し、wwwroot フォルダに配置したファイルを静的ファイルとしてアクセス可能な状態にします。
実行結果
上記のプロジェクトを実行します。
WebブラウザでアプリケーションルートのURLにアクセスします。静的ファイルではなく、Webアプリケーションのレスポンスである、"Hello World!" のメッセージが表示されます。
(アプリケーションルートURL)/index.html
のURLにアクセスします。静的ファイルのindex.htmlのページが表示されます。
(アプリケーションルートURL)/default.html
のURLにアクセスします。同様に静的ファイルのdefault.htmlのページが表示されます。
(アプリケーションルートURL)/subdir/start.html
のURLにアクセスします。サブディレクトリの start.html のページが表示されます。
(アプリケーションルートURL)/subdir/
のURLにアクセスします。デフォルトドキュメントの設定が無いため、ページが見つからない、404エラーが戻ります。
静的ファイルをURLで直接表示した場合はファイルが表示できますが、ディレクトリ名のみを指定した場合は、デフォルトドキュメントの設定が無いため、
Not Foundエラーになってしまう動作が確認できました。
DefaultFilesOptions を指定せずに UseDefaultFiles() を呼び出した場合の動作
コード
下記のコードに変更します。
UseDefaultFiles()
メソッドの呼び出しを追記します。
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;
namespace StaticFileDefaultDocument
{
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)
{
}
// 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.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}
解説
app.UseDefaultFiles() メソッドを呼び出し、wwwroot フォルダに配置したファイルのデフォルトドキュメントが動作する状態にします。
実行結果
上記のプロジェクトを実行します。
アプリケーションルートのURLにアクセスします。Webアプリケーションのレスポンスではなく、静的ファイルの default.html のページが表示されます。
(アプリケーションルートURL)/subdir/
のURLにアクセスします。下図のページが表示されます。静的ファイルの、/subdir/default.html のページが表示されます。
default.html のページをリネーム、または削除した場合、index.html の静的ファイルのページが表示されます。
DefaultFilesOptions を指定しなかった場合は、DefaultFilesOptions オブジェクトを作成したデフォルトの状態が設定される動作となります。
DefaultFilesOptions を生成した直後の状態を確認すると、以下の設定がされています。
dfo.DefaultFileNames
[0] "default.htm" string
[1] "default.html" string
[2] "index.htm" string
[3] "index.html" string
デフォルトのドキュメントの検索順は上から次の通りになります。
- default.htm
- default.html
- index.htm
- index.html
デフォルトのドキュメント名を変更する場合は次のセクションの DefaultFilesOptions を指定するコードを利用します。
DefaultFilesOptions を指定して UseDefaultFiles() を呼び出した場合の動作
コード
下記のコードを記述します。
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;
namespace StaticFileDefaultDocument
{
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)
{
}
// 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();
}
DefaultFilesOptions dfo = new DefaultFilesOptions();
dfo.DefaultFileNames.Clear();
dfo.DefaultFileNames.Add("start.html");
app.UseDefaultFiles(dfo);
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}
解説
DefaultFilesOptions オブジェクトを作成します。
作成直後は、デフォルトのドキュメントが
default.html
default.htm
index.html
index.htm
が指定されているため、
Clear()
メソッドを呼び出し、
デフォルトドキュメントのリストをクリアします。クリア後に
start.html
をデフォルトドキュメントとして追加します。
DefaultFilesOptions dfo = new DefaultFilesOptions();
dfo.DefaultFileNames.Clear();
dfo.DefaultFileNames.Add("start.html");
UseDefaultFiles
メソッドを呼び出します。メソッドの第一引数に、先に作成した DefaultFilesOptions オブジェクトを与えます。
app.UseDefaultFiles(dfo);
実行結果
プロジェクトを実行します。アプリケーションルートのURLにアクセスします。デフォルトドキュメントが start.html に設定されているため、
静的ファイルの start.html が表示されます。
(アプリケーションルートURL)/subdir/
のURLにアクセスします。下図のページが表示されます。静的ファイルの、/subdir/sart.html のページが表示されます。
デフォルトドキュメントの設定が変更されており、start.html がデフォルトドキュメントとして表示されます。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2021-06-20
作成日: 2021-06-20