ASP.NET Core .NET6 以降の ASP.NET Core アプリケーションで AddRazorPagesOptionsメソッドで設定するコードを紹介します。
概要
.NET5以前のコードでは、RazorPagesのルーティングの追加は、ConfigureServices()メソッド内で、AddRazorPagesOptions()メソッドを呼び出し、
RazorPagesOptions
オブジェクトをパラメーターに持つ
Action<RazorPagesOptions>
を実装し、デリゲート内で、
RazorPagesOptionsオブジェクトの
Conventions.AddPageRoute()
メソッドを呼び出して、ルーティングを設定しました。
.NET6以降のASP.NET Coreのコードでは、
ConfigureServices()
メソッドの記述がなくなるため、設定の記述が変化します。
.NET5以前のコード
.NET5以前のコードは以下になります。
ルーティングの設定は、
ConfigureServices()
メソッド内で
IMvcBuilder
オブジェクトの
AddRazorPagesOptions()
メソッドを呼び出し、
メソッドに与えたデリゲート内で、
RazorPagesOptions
オブジェクトの
Conventions.AddPageRoute()
メソッドを呼び出して設定します。
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using iPentec.Document.Image;
namespace iPentecSampleApp
{
public class Startup
{
private IConfiguration _config;
public Startup(IConfiguration config)
{
_config = config;
}
// 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)
{
IMvcBuilder builder = services.AddRazorPages();
builder.AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/sample01", "/test/{value:int?}");
options.Conventions.AddPageRoute("/sample02", "/test/{action:regex(aaa|bbb|ccc)}/{query?}");
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
// 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("/sample", async context =>
{
/* 実装 */
});
endpoints.MapRazorPages();
});
}
}
}
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BlankAppAspNetCore5vs2022
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
.NET6以降のコード
.NET6以降では、Program.csファイルのみのコードになり、Startupクラスもなくなり、
ConfigureServices()
メソッドは生成されなくなります。
RazorPagesOptions
の設定は、WebApplicationオブジェクトの
Services.AddRazorPages()
メソッドの呼び出し時に、第一引数に、RazorPagesOptions オブジェクトを第一引数に取る
Action<RazorPagesOptions>
デリゲートを与え、デリゲート内で、RazorPagesOptions オブジェクトの
Conventions.AddPageRoute()
メソッドを呼び出します。
namespace iPentecSampleApp
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddRazorPages(options =>
{
options.Conventions.AddPageRoute("/sample01", "/test/{value:int?}");
options.Conventions.AddPageRoute("/sample02", "/test/{action:regex(aaa|bbb|ccc)}/{query?}");
});
var app = builder.Build();
app.UseStaticFiles();
app.MapGet("/sample", () => "Hello World!");
app.MapRazorPages();
app.Run();
}
}
}
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-02-13
作成日: 2024-02-12