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()
这里无效
Install-package Microsoft.EntityFrameworkCore.Tools
Install-package Microsoft.EntityFrameworkCore.Sqlite
PM> Scaffold-DbContext "Data Source=.\database.db" Microsoft.EntityFrameworkCore.Sqlite -OutputDir Models