Skip to content

Instantly share code, notes, and snippets.

@trashvin
Last active June 1, 2017 03:51
Show Gist options
  • Save trashvin/fe502097e3b307aa041a82a4b2054495 to your computer and use it in GitHub Desktop.
Save trashvin/fe502097e3b307aa041a82a4b2054495 to your computer and use it in GitHub Desktop.
Enabling CORS in ASPNet Core
using Microsoft.AspNetCore.Cors; //add reference to the needed library
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
//Add CORS service beloe
services.AddCors( options => options.AddPolicy("AllowCors",
builder => {
builder.AllowAnyOrigin()
.WithMethods("GET","PUT","POST","DELETE")
.AllowAnyHeader();
}
));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
//Add CORS
app.UseCors("AllowCors");
}
}
// Add reference
using Microsoft.AspNetCore.Cors;
namespace Sampl.Controllers
{
//Enable CORS
[EnableCors("AllowCors"), Route("api/[controller]")]
public class ValuesController : Controller
{
// Code here...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment