asp.net-core – 如何在我的ASP.NET核心WebApi项目中全局启用CORS

我正在尝试将我的Web Api2迁移到ASP.NET核心web api项目.在我的项目中,我们使用的是EnableCors功能.

我在Microsoft网站上找到了这个文档,我将其用作参考 – https://docs.asp.net/en/latest/security/cors.html

正如“在MVC中启用CORS”一节中所述,我试图在这样的ConfigureServices menthod中全局启用cors –

services.Configure<Mvcoptions>(options =>
            {
                options.Filters.Add(new CorsAuthorizationFilterFactory("AllowSpecificOrigin"));
            });

但是,我收到了这个我无法理解的错误

The call is ambiguous between the following methods or properties:
‘Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure(Microsoft.Extensions.DependencyInjection.IServiceCollection,
System.Action)’ and
‘Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure(Microsoft.Extensions.DependencyInjection.IServiceCollection,
System.Action)’

请参阅错误截图 – ERROR scrrenshot

有谁能告诉我如何在我的ASP.NET核心WebApi项目中全局启用CORS?

Project.json:

{
  "userSecretsId": "aspnet5-MVC6","version": "1.4.0-*","buildOptions": {
    "emitEntryPoint": true,"preserveCompilationContext": true
  },"dependencies": {
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0","Microsoft.AspNetCore.Authentication.Cookies": "1.0.0","Microsoft.AspNetCore.Diagnostics": "1.0.0","Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0","Microsoft.AspNetCore.Identity": "1.0.0","Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0","Microsoft.AspNetCore.Mvc": "1.0.0","Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0","Microsoft.AspNet.Cors": "6.0.0-rc1-final","Microsoft.AspNetCore.Server.IISIntegration": "1.0.0","Microsoft.AspNetCore.Server.Kestrel": "1.0.0","Microsoft.AspNetCore.StaticFiles": "1.0.0","Microsoft.EntityFrameworkCore": "1.0.0","Microsoft.EntityFrameworkCore.sqlServer": "1.0.0","Microsoft.Extensions.Configuration.CommandLine": "1.0.0","Microsoft.Extensions.Configuration.FileExtensions": "1.0.0","Microsoft.Extensions.Configuration.Json": "1.0.0","Microsoft.Extensions.Configuration.UserSecrets": "1.0.0","Microsoft.Extensions.Logging": "1.0.0","Microsoft.Extensions.Logging.Console": "1.0.0","Microsoft.Extensions.Logging.Debug": "1.0.0","Microsoft.VisualStudio.Web.browserLink.Loader": "14.0.0"
  },"tools": {
    "BundlerMinifier.Core": "2.0.238","Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final","Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final","Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final"
  },"frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50"
      ],"dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform","version": "1.0.0"
        }
      }
    },"net461": {
      "dependencies": {
        "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview2-final"
      }
    }
  },"publishOptions": {
    "exclude": [
      "**.user","**.vspscc","wwwroot","node_modules"
    ]
  },"scripts": {
    "prepublish": [ "npm install","bower install","gulp clean","gulp min" ]
  }
}

解决方法

你混淆了依赖关系.

“Microsoft.AspNet.Cors”: “6.0.0-rc1-final”

一个非常旧的版本,导致您的解决方案加载了两个具有相同名称空间和类型的不同程序集,编译器不知道使用哪个程序集.

将其更改为

“Microsoft.AspNetCore.Cors”: “1.0.0”

所有Microsoft.AspNet.*包都非常陈旧,不应使用.他们都用RC2重命名为Microsoft.AspNetCore.*

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....