如何为我的dot net core web API启用CORS [英] How do I get to enable CORS for my dot net core web API

查看:127
本文介绍了如何为我的dot net core web API启用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过我正在开发的移动应用程序(使用框架7)访问我本地IIS上托管的web api。当我尝试说GET或POST请求并且我在chrome上的ripple模拟器上测试应用程序时,我得到

i am trying to access my web api hosted on my local IIS through a mobile application(using framework 7) i'm developing. When i try to do say a GET or POST request and i test the application on my ripple emulator on chrome, i get the

"No 'Access-Control-Allow-Origin' header is present on the requested resource."

错误。我试图将CORS添加到我的启动文件中,我将在下面显示,但我仍然无法使其工作。热烈欢迎回复



我尝试过:



error . I have tried to add CORS to my start up file which i would show below but i still can't get it to work. A response would be highly welcomed

What I have tried:

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });



            services.AddCors(options => options.AddPolicy("AllowCors", p => p.AllowAnyOrigin()
                                                                   .AllowAnyMethod()
                                                                   .AllowCredentials()
                                                                    .AllowAnyHeader()));
            services.Configure<IISOptions>(options =>
            {
                options.ForwardClientCertificate = false;
            });            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }




public void Configure(IApplicationBuilder app, IHostingEnvironment env)
       {

           if (env.IsDevelopment())
           {
               app.UseDeveloperExceptionPage();
           }
           else
           {
               app.UseExceptionHandler("/Home/Error");
               app.UseHsts();
           }
           app.UseCors("AllowCors");
           app.UseHttpsRedirection();
           app.UseDefaultFiles();
           app.UseStaticFiles();
           app.UseCookiePolicy();

           //app.UseMiddleware<RequestResponseLoggingMiddleware>();
           app.UseMvc(routes =>
           {
               routes.MapRoute(
                   name: "default",
                   template: "{controller=Account}/{action=Login}/{id?}");
           });
       }







i还启用了控制器上的CORS





i have also enabled CORS on the controller

[EnableCors("AllowCors")]
[Route("api/[controller]")]
[ApiController]
public class TenantController : ControllerBase
{

推荐答案

在Startup.cs中使用以下代码



Use this below code in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
     services.AddCors();
     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  if (env.IsDevelopment())
  {
     app.UseDeveloperExceptionPage();
  }
   else
  {
     app.UseHsts();
  }

  app.UseCors(builder =>
  builder
  .AllowAnyOrigin()
  .AllowAnyHeader()
  .AllowAnyMethod()
  );

  app.UseHttpsRedirection();
  app.UseMvc();
}


这篇关于如何为我的dot net core web API启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆