由于某些原因我不想使用 UsePathBase() 。我尝试使用 MapExtensions.Map() 如下所示。但这不起作用。当我请求“/api/v1/WeatherForecast”时,响应“404 Not Found”。顺便说一下,这个端点 url 是“/WeatherForecast”,但也响应“404 Not Found”。
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
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 configuration = builder.Configuration.
AddEnvironmentVariables()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.Build();
var app = builder.Build();
app.UseDefaultFiles();
app.UseStaticFiles();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.Map("/api/v1", apiApp =>
{
apiApp.UseRouting();
apiApp.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
});
app.MapFallbackToFile("/index.html");
app.Run();
我想通过app.Map()设置根路径。
PathBase
(通过 UsePathBase
通话):
app.UsePathBase("/api");
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
请注意,这将使您的控制器可通过
api/controller
和 controller
路线使用。