【发布时间】:2016-02-07 08:35:01
【问题描述】:
我最近阅读了这段代码,它使 MVC Web API 允许 CORS(跨源资源共享)。我知道ActionFilterAtrribute 使它成为一个过滤器,但我不确定这个类发生了什么:AllowCORS。
public class AllowCORS : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(filterContext.HttpContext.Request.HttpMethod == "OPTIONS")
{
filterContext.Result = new EmptyResult();
}
else
{
base.OnActionExecuting(filterContext);
}
}
}
所以基本上,如果我们收到的请求方法是HttpOPTIONS,我们会做一些我在这种情况下不太理解的事情。否则,我们会做一些我也不确定的事情吗?
有人能提供帮助和详细说明吗?这里到底发生了什么?
【问题讨论】:
标签: c# asp.net-mvc cross-domain actionfilterattribute onactionexecuting