Click here to Skip to main content
14,766,544 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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."
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 have also enabled CORS on the controller

[EnableCors("AllowCors")]
[Route("api/[controller]")]
[ApiController]
public class TenantController : ControllerBase
{
Posted
Updated 22-Nov-18 23:35pm

1 solution

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();
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900