Last active
June 1, 2017 03:51
-
-
Save trashvin/fe502097e3b307aa041a82a4b2054495 to your computer and use it in GitHub Desktop.
Enabling CORS in ASPNet Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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