简体   繁体   English

如何在同一进程中实现 ASP.NET 核心 Blazor 服务器应用程序和 Web API 应用程序?

[英]Howto implement ASP.NET Core Blazor Server app and a Web API app in the same procress?

I want to implement both我想同时实施

  • a ASP.NET Core Blazor Server一个 ASP.NET 核心 Blazor 服务器
  • and a ASP.NET Core Web API (the server part, not the consumer/client)和一个 ASP.NET 核心 Web API(服务器部分,不是消费者/客户端)

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) 为我创建的两个模板:

For the Blazor Server App:对于 Blazor 服务器应用程序:

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();

For the ASP.NET Core Web API App:对于 ASP.NET Core Web API App:

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:我想要:

  • Have my program listening to a single port让我的程序监听一个端口
  • Host the Blazor web pages主机 Blazor web 页
  • But process the API when the URL myhost:port/api/.. is being accessed (without interfering with the Blazor part)但是在访问 URL myhost:port/api/..时处理 API(不干扰 Blazor 部分)
  • Have the SwaggerUI, preferably under myhost:port/api/swagger/index.html (again without interfering with the Blazor part)拥有 SwaggerUI,最好在myhost:port/api/swagger/index.html 下(再次不干扰 Blazor 部分)
  • Use the same security mechanism based on a client certificate for both对两者使用基于客户端证书的相同安全机制

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.

相关问题 是否有示例在ASP.NET Web API(不是.net核心)中实现App.Metrics? - Is there an example to implement App.Metrics in ASP.NET Web API (not .net core)? Blazor 应用服务器 - 在类似于 asp.net 核心 Mvc 的 blazor 中使用声明? - Blazor App Server - Using Claims in a blazor similar to asp.net core Mvc? 如何将 Blazor Web 程序集与 ASP.NET 核心托管和身份验证应用程序发布到 ZCF04A02E37B774FC3917A 服务? - How to publish Blazor Web assembly with ASP.NET core hosting and identity auth app to azure App Service? 从控制台应用程序启动 ASP.NET Core Web API - Start ASP.NET Core Web API from a Console App Asp.net 核心 3.1 保护 API 和 web 应用程序 - Asp.net core 3.1 securing API and web app ASP.NET 内核 Web API 应用内多数据库 - ASP.NET Core Web API multi database in app How to map fallback in ASP .NET Core Web API so that Blazor WASM app only intercepts requests that are not to the API - How to map fallback in ASP .NET Core Web API so that Blazor WASM app only intercepts requests that are not to the API 如何通过 IdentityServer4 将 OpenIdConnect 添加到 ASP.NET Core ServerSide Blazor Web 应用程序? - How to add OpenIdConnect via IdentityServer4 to ASP.NET Core ServerSide Blazor web app? 部署托管到 Azure 的 Blazor WebAssembly 应用 ASP.NET Core - Deploy Blazor WebAssembly App ASP.NET Core hosted to Azure 如何导航到 Blazor 应用程序中的 ASP.NET Core MVC 控制器? - How to navigate to an ASP.NET Core MVC controller in Blazor app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM