title: Howto implement ASP.NET Core Blazor Server app and a Web API app in the same procress?
body: <p>I want to implement both</p>
  <ul>
  <li>a ASP.NET Core Blazor Server</li>
  <li>and a ASP.NET Core Web API (the server part, not the consumer/client)</li>
  </ul>
  <p>in the <em>same</em> process using .NET 6 and run it self-hosted with Kestrel, i.e. without IIS.</p>
  <p>I assume the key is the service and middleware pipeline configuration as found in the according Program.cs templates. Here are the two templates that VS 2022 (17.1.5) creates for me:</p>
  <h2>For the Blazor Server App:</h2>
  <pre><code>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&lt;WeatherForecastService&gt;();
  
  var app = builder.Build();
  
  // Configure the HTTP request pipeline.
  if (!app.Environment.IsDevelopment())
  {
    app.UseExceptionHandler(&quot;/Error&quot;);
    // 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(&quot;/_Host&quot;);
  
  app.Run();
  </code></pre>
  <h2>For the ASP.NET Core Web API App:</h2>
  <pre><code>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();
  </code></pre>
  <p>So the question is how can I combine these two into one?</p>
  <p>I want to:</p>
  <ul>
  <li>Have my program listening to a single port</li>
  <li>Host the Blazor web pages</li>
  <li>But process the API when the URL <em>myhost:port/api/..</em> is being accessed (without interfering with the Blazor part)</li>
  <li>Have the SwaggerUI, preferably under <em>myhost:port/api/swagger/index.html</em> (again without interfering with the Blazor part)</li>
  <li>Use the same security mechanism based on a client certificate for both</li>
  </ul>