微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.*

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐