asp.net core web api + vue(一)

调试环境注意

1,配置launchSettings.json,修改为本机IP

 

2,采用的Server是 Kestrel

vue 访问webapi 405解决

跨域

1、nuget要加上 Microsoft.AspNetCore.Cors 中间件。

2、在Startup类里先定义一个全局变量。

private readonly string AllowSpecificOrigin = "AllowSpecificOrigin";

3、在Startup的ConfigureServices中添加以下代码来配置跨域处理。

 1 #region 跨域
 2 services.AddCors(options =>
 3 {
 4     options.AddPolicy(AllowSpecificOrigin,
 5         builder =>
 6         {
 7             builder.AllowAnyMethod()
 8                 .AllowAnyOrigin()
 9                 .AllowAnyHeader();
10         });
11 });
12 #endregion

3、在Startup的Configure中添加以下代码来配置跨域处理。

app.UseRouting();
//CORS 中间件必须配置为在对 UseRouting 和 UseEndpoints的调用之间执行。 配置不正确将导致中间件停止正常运行。
app.UseCors(AllowSpecificOrigin);
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

 

  

 

posted @ 2021-01-11 13:49  YuZhou00x0  阅读(327)  评论(0)    收藏  举报