【发布时间】:2015-10-29 07:44:11
【问题描述】:
我正在尝试在我的 MVC 6 WebApi 项目中启用 CORS,我发现了这个问题,专注于同样的问题: How do you enable cross-origin requests (CORS) in ASP.NET 5 & MVC 6?
看起来和我想做的一模一样,但是当我到达这部分时,我遇到了麻烦。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//Add Cors support to the service
services.AddCors();
var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy();
policy.Headers.Add("*");
policy.Methods.Add("*");
policy.Origins.Add("*");
policy.SupportsCredentials = true;
services.ConfigureCors(x=>x.AddPolicy("mypolicy", policy));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Configure the HTTP request pipeline.
app.UseStaticFiles();
//Use the new policy globally
app.UseCors("mypolicy");
// Add MVC to the request pipeline.
app.UseMvc();
}
它(以及我发现的有关此主题的任何其他资源)明确指出我应该在Startup.cs 文件的Configure 方法中使用app.UseCors("policyname")。
但是当我这样做时,我收到以下错误:
'IApplicationBuilder' does not contain a definition for 'UseCors' and no extension method 'UseCors' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
我知道这是 MVC 6 中的一个全新功能,也许这可以解释为什么我无法找到任何有关如何实现此功能的官方文档。
是否可以像添加正确的 NuGet 包一样简单? 我尝试了几个 Microsoft 的与 CORS 相关的 .Net NuGet 包,但都没有解决问题,我怀疑它们与旧版本的 WebApi 有关。
【问题讨论】:
标签: c# asp.net-web-api cors