var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// 追加
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting(); // 追加
app.UseAuthorization(); // 追加(認証関係なければ無くても良い)
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.MapRazorPages(); // 追加(<AppRoot>/Pages下をマッピング)
app.Run();
public class SingletonClass {
public int Count { get; set; }
public EventHandler? SingletonEvent { get; set; }
public void BubbleSingletonEvent(object? sender, EventArgs e) {
if (SingletonEvent != null) {
SingletonEvent.Invoke(sender,e);
}
}
}
public class ScopedClass {
public int Count { get; set;}
}
public class TransientClass {
public int Count { get; set;}
}
これらのクラスをサービスとして登録
・・・
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// Singleton
builder.Services.AddSingleton<SingletonClass>();
// Scoped
builder.Services.AddScoped<ScopedClass>();
// Transient
builder.Services.AddTransient<TransientClass>();
・・・