Razor Page と 静的ファイルのルーティング順序 - ASP.NET Core

Razor Page と 静的ファイルのルーティング順序について紹介します。

動作の確認

ASP.NET Core アプリケーションを作成します。
以下のRazor Pageを作成します。
@page ディレクティブで "/app" が指定されているため、 (アプリケーションルートURL)/app でこのRazorPageが表示されます。
@page "/app"
@model RazorPagesAndStaticFile.Pages.TestPage01Model
@{
}
<html>
  <head>
  </head>
  <body>
    <p>Razor Pageのテストページです。</p>
  </body>
</html>

続いて wwwroot フォルダを作成し、フォルダ内に app フォルダを作成し、appフォルダ内にindex.html ファイルを配置します。
appフォルダへのアクセス時にデフォルトドキュメントにindex.htmlが指定されていれば (アプリケーションルートURL)/app でこのindex.htmlファイルが表示されます。
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
  <p>静的HTMLファイルのテストページです。</p>
</body>
</html>

静的ファイルとRazorPageで同じURLで競合しています。この時の動作を確認します。
下記の Startup.cs ファイルを記述します。
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 RazorPagesAndStaticFile
{
  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();
      app.UseDefaultFiles();
      app.UseStaticFiles();

      app.UseEndpoints(endpoints =>
      {
        endpoints.MapRazorPages();
      });
    }
  }
}

RazorPageのルーティングが無い場合

Configureメソッドを下記に変更し、RazorPages のルーティングが無い場合の動作を確認します。
app.UseDefaultFiles();を設定しないと、ディレクトリへのアクセス時に index.html ファイルがデフォルトドキュメントとして表示されないため、 記述する必要があります。
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
      }

      app.UseRouting();
      app.UseDefaultFiles();
      app.UseStaticFiles();
    }

プロジェクトを実行し、(アプリケーションルートURL)/app URLにアクセスします。下図のページが表示されます。
静的HTMLのページが表示されます。

静的ファイルのルーティングが先に記述されている場合

続いて、app.UseEndpoints を記述しRazorPagesへのルーティングを追加した場合の動作を確認します。 Configureメソッドを下記のコードに変更します。一般的なコードの記述です。
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
      }

      app.UseRouting();
      app.UseDefaultFiles();
      app.UseStaticFiles();
      
      app.UseEndpoints(endpoints =>
      {
        endpoints.MapRazorPages();
      });
    }

プロジェクトを実行し、(アプリケーションルートURL)/app URLにアクセスします。下図のページが表示されます。
Razor Page のページが表示されます。

静的ファイルのルーティングが後に記述されている場合

UseDefaultFiles(), UseStaticFiles() メソッドをUseEndpoints メソッドの後ろに記述した場合の動作も確認します。
下記のコードに変更します。
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
      }

      app.UseRouting();
   
      app.UseEndpoints(endpoints =>
      {
        endpoints.MapRazorPages();
      });
   
      app.UseDefaultFiles();
      app.UseStaticFiles();
    }

プロジェクトを実行し、(アプリケーションルートURL)/app URLにアクセスします。下図のページが表示されます。
コードの順番を並び替えた場合でも、Razor Page のページが表示されます。


RazorPagesと静的ファイルでURLが競合する場合は、RazorPagesのルーティングが優先される動作になっています。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2021-05-09
作成日: 2021-05-09
iPentec all rights reserverd.