我有使用CorsFeature服务的设置,以及正在使用的文件APPHOST使用的函数收集到mythz在其他的答案提出的办法,:
private void ConfigureCors(Funq.Container container)
{
Plugins.Add(new CorsFeature(allowedOrigins: "*",
allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
allowedHeaders: "Content-Type, Authorization, Accept",
allowCredentials: true));
PreRequestFilters.Add((httpReq, httpRes) =>
{
//Handles Request and closes Responses after emitting global HTTP Headers
if (httpReq.HttpMethod == "OPTIONS")
{
httpRes.EndRequest();
}
});
}
Run Code Online (Sandbox Code Playgroud)
但是,预请求过滤器仅针对某些服务请求触发.我们在服务中拥有的一个基本实体是一个问题实体,并且定义的自定义路由如下:
[Route("/question")]
[Route("/question/{ReviewQuestionId}", "GET,DELETE")]
[Route("/question/{ReviewQuestionId}/{ReviewSectionId}", "GET")]
Run Code Online (Sandbox Code Playgroud)
使用POSTMAN触发测试查询(全部使用OPTIONS动词),我们可以看到这将触发预请求过滤器:
http://localhost/myservice/api/question/
Run Code Online (Sandbox Code Playgroud)
但这不会:
http://localhost/myservice/api/question/66
Run Code Online (Sandbox Code Playgroud)
据推测,这是因为第二和第三条路线明确定义了它们接受的动词,OPTIONS不是其中之一.
是否真的有必要在限制所支持的动词的每个定义路线中拼出OPTIONS?
servicestack ×1