I'd like to enable CORS on an API built with ASP.NET Core MVC, but all the current documents refer to earlier versions of that framework.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
The notes on the new Cors features are very light, but I was able to get it working in my solution by looking at the new classes and methods. My Web API startup.cs looks like this. You can see how you can construct your origins and policies her by using the new
CorsPolicy
class. And enabling CORS with theAddCors
andUseCors
methods.You can also reference the policy in the controllers with the new attributes like so
I got it working using the following code:
You can chain
AllowAnyHeader()
and/orAllowAnyMethod()
to the configure action if needed.To configure it for the complete app:
Or just for a controller:
--
Update: configuring CORS for all requests can be done a bit easier:
For more information, refer to the docs.
cs1929 the method services.ConfigureCors(...) does no more exist. It is combined to AddCors:
In the most recent
RC2
of ASP.NET Core.The NuGet packages are
In Startup.cs
Support for CORS is currently in development. Following issue is tracking that: https://github.com/aspnet/Mvc/issues/498
Update (3/28/2015):
This feature has been checked in and should be available in the next release.
Install : Microsoft.AspNetCore.Cors
In Configure method: