Last active
December 24, 2015 23:09
-
-
Save jayhjkwon/6878275 to your computer and use it in GitHub Desktop.
CORS support in ASP.NET WebAPI2
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
Install-Package Microsoft.AspNet.WebApi.Cors -pre -project WebService |
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 System.Net.Http; | |
using System.Web.Http; | |
using System.Web.Http.Cors; | |
namespace WebService.Controllers | |
{ | |
[EnableCors(origins: "http://myclient.azurewebsites.net", headers: "*", methods: "*")] | |
public class TestController : ApiController | |
{ | |
// Controller methods not shown... | |
} | |
} |
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 System.Web.Http; | |
namespace WebService | |
{ | |
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
// New code | |
config.EnableCors(); | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment