是否可以手动预检多个 CORS 资源?
Posted
技术标签:
【中文标题】是否可以手动预检多个 CORS 资源?【英文标题】:Is it possible to manually preflight multiple CORS resources? 【发布时间】:2016-07-17 14:22:48 【问题描述】:所以this 明确表示您需要对服务器上的每个资源单独的 CORS 预检请求,因为我正在使用需要自定义标头(以及内容类型 JSON)的 RESTFul API。但是,我想避免(几乎)每个请求都进行两次往返。
我理想的解决方案是在一个请求中预检多个资源。因此,如果我想允许 Web 应用程序 POST 到 /user
、/media
和 /preferences
,我们可以预先在一个请求中执行此操作,甚至在这些请求第一次发出之前,否则会增加可怕的延迟。使用 CORS 可以做到这一点吗?
【问题讨论】:
【参考方案1】: public static class WebApiConfig
public static void Register(HttpConfiguration config)
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
//config.SuppressDefaultHostAuthentication();
//config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
//var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
// xml.UseXmlSerializer = true;
//var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
//json.SerializerSettings.PreserveReferencesHandling =
// Newtonsoft.Json.PreserveReferencesHandling.All;
//json.SerializerSettings.DateFormatHandling
//= Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/controller/id",
defaults: new id = RouteParameter.Optional
);
//And on WEBAPI your controller try this.
[EnableCorsAttribute("http://localhost:8080/", "*", "*")]
public class SampleController : ApiController
// GET: api/Club
public IEnumerable<Sample> Get()
var SampleRepository = new SampleRepository();
return SampleRepository.Retrieve();
// GET: api/Sample/id
public string Get(int id)
return "value";
// POST: api/sample
public void Post([FromBody]string value)
// PUT: api/sample/id
public void Put(int id, [FromBody]string value)
// DELETE: api/sample/id
public void Delete(int id)
//In AngularJS you will get what you want this way using CORS through WEB API
(function ()
"use strict";
angular
.module('sampleManagement')
.controller('SampleListController',
['sampleResource',
SampleListController]);
function SampleListController(sampleResource)
var vm = this;
sampleResource.query(function (data)
vm.Sample = data;
);
());
(function ()
"use strict";
var app = angular.module('sampleManagement',
['common.services']);
());
(function ()
"use strict";
angular.module('common.services',
['ngResource'])
.constant('appSettings',
serverPath: "http://localhost:8080/"
);
());
(function ()
"use strict";
angular
.module('common.services')
.factory('sampleResource',
['$resource',
"appSettings",
sampleResource])
function sampleResource($resource, appSettings)
return $resource(appSettings.serverPath + "api/Club/:id");
app.config(['$resourceProvider', function($resourceProvider)
$resourceProvider.defaults.stripTrailingSlashes = false;
]);
());
【讨论】:
以上是关于是否可以手动预检多个 CORS 资源?的主要内容,如果未能解决你的问题,请参考以下文章
如何为 CORS 预检请求绕过 AWS API Gateway 代理资源上的 Cognito 授权方?