namespace WebApplication2
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSpaStaticFiles(configuration: options => { options.RootPath = "wwwroot"; });
builder.Services.AddControllers();
builder.Services.AddCors(options =>
{
options.AddPolicy("VueCorsPolicy", cfg =>
{
cfg
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin();
});
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseHsts();
}
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
app.UseCors("VueCorsPolicy");
app.UseSpa(spa =>
{
if (app.Environment.IsDevelopment())
{
spa.UseProxyToSpaDevelopmentServer("http://localhost:5173");
}
});
app.Run();
}
}
}
- 必须使用
app.UseRouting();app.UseEndpoints(endpoints => endpoints.MapControllers());
app.MapControllers()
这里无效