【发布时间】:2014-08-06 20:59:58
【问题描述】:
我有 CORS 使用以下内容:
[System.Web.Http.HttpPut]
[System.Web.Http.AcceptVerbs("OPTIONS")]
[System.Web.Http.Route("api/exercise")]
public HttpResponseMessage UpdateExercise(Exercise exercise) {
try {
_adminService.UpdateExercise(exercise);
return Request.CreateResponse(HttpStatusCode.OK, "Success");
} catch (Exception e) {
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
在我的global.asax:
protected void Application_BeginRequest() {
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS") {
Response.Flush();
}
}
但是发生了一些奇怪的事情 - 如果我在控制器中设置断点,则 OPTIONS 请求会通过 null 练习进入内部。为什么会这样?我希望Flush() 可以防止这种情况发生。
就目前而言,我需要为所有对 CORS 敏感的端点(PUT、DELETE)添加对 null 的检查。这似乎不优雅...我是否应该能够阻止 OPTIONS 请求访问控制器逻辑,而不是直接使用所需的标头进行响应?
【问题讨论】:
-
@CarlHancke - 成功了。看起来我已经实现了另一种方法的一半......随时发布作为答案,非常感谢:)
标签: asp.net-web-api cors asp.net-web-api2 http-options-method