.net core1.1 跨域请求设置

.net core 跨域请求设置

在.net core 中使用是相当简单的

  • 项目中安装

    • 使用的是 .net core1.1
    • Microsoft.AspNetCore.Cors
    • 使用的地方当然也需要using
  • startup.cs 下的 ConfigureServices 类中设置

#region 设置跨域请求
var urls = "http://localhost:/"; // 还没有研究这个什么我设置的是我的访问路径
services.AddCors(options =>
  options.AddPolicy("MyDomain",
 builder=>builder.WithOrigins(urls).AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin().AllowCredentials()));
#endregion
  • Controller 中使用
[EnableCors("MyDomain")]
public IActionResult TabData()
{
    var v = new { Id = 1, Name = "name"};
    return Json(v);
}

你可能感兴趣的:(.net core1.1 跨域请求设置)