[英]Howto implement ASP.NET Core Blazor Server app and a Web API app in the same procress?
I want to implement both我想同时实施
in the same process using .NET 6 and run it self-hosted with Kestrel, ie without IIS.在同一进程中使用 .NET 6 并使用 Kestrel 自托管运行它,即没有 IIS。
I assume the key is the service and middleware pipeline configuration as found in the according Program.cs templates.我认为关键是在相应的 Program.cs 模板中找到的服务和中间件管道配置。 Here are the two templates that VS 2022 (17.1.5) creates for me:
这是 VS 2022 (17.1.5) 为我创建的两个模板:
using BlazorApp1.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// 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.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
So the question is how can I combine these two into one?所以问题是如何将这两者合而为一?
I want to:我想要:
You should be able to combine these without any issues:您应该能够毫无问题地组合这些:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthorization();
app.UseRouting();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
The service configuration supports registring all dependencies anyway.无论如何,服务配置支持注册所有依赖项。 And between
AddRazorPages()
and AddController()
, there are quite a lot of shared ones anyway.在
AddRazorPages()
和AddController()
之间,无论如何都有很多共享的。
The only thing that can be tricky is the pipeline configuration since you need to make sure that both API requests and Blazor requests are handled by the correct handlers.唯一棘手的是管道配置,因为您需要确保 API 请求和 Blazor 请求都由正确的处理程序处理。
The good thing is that MapControllers
is usually means a fixed set of routes.好消息是
MapControllers
通常意味着一组固定的路由。 Since your controller actions have specific routes, having the MapControllers
first will make sure that those specific routes will be handled properly by your controllers.由于您的 controller 操作具有特定路线,因此首先使用
MapControllers
将确保这些特定路线将由您的控制器正确处理。
All other requests, those that don't match any controller action, will then go to the next route handler in the pipeline.所有其他请求,那些不匹配任何 controller 操作的请求,然后将 go 发送到管道中的下一个路由处理程序。 So
MapBlazorHub
is next which will host the SignalR hub for Blazor. That's also a very specific route which is provided here.因此
MapBlazorHub
是下一个,它将托管 Blazor 的 SignalR 集线器。这也是此处提供的非常具体的路由。
So finally, all remaining requests will land at MapFallbackToPage()
.所以最后,所有剩余的请求都将到达
MapFallbackToPage()
。 This is a fallback-handler which means that it will handle any route.这是一个后备处理程序,这意味着它将处理任何路由。 This allows Blazor to have a single entry-point for which it will then use client-side routing.
这允许 Blazor 有一个入口点,然后它将使用客户端路由。 As long as this fallback call is the last in the pipeline, it should not be able to interfere with any other route handler.
只要这个回退调用是管道中的最后一个,它就不能干扰任何其他路由处理程序。
With that combined configuration, both your API and your Blazor should work just fine.使用该组合配置,您的 API 和 Blazor 都应该可以正常工作。
If you do have a situation which is more complicated, then you can use app.MapWhen to branch off the pipeline.如果您确实遇到了更复杂的情况,那么您可以使用app.MapWhen来分支管道。 There is an example in the documentation if you are interested in this functionality.
如果您对此功能感兴趣, 文档中有一个示例。 But as I said, for your situation, you shouldn't need it.
但正如我所说,对于你的情况,你不应该需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.