我最近阅读了这段代码,它使 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);
}
}
}
所以基本上,如果我们收到的请求方法是 aHttpOPTIONS
我们会做一些我在这种情况下不太理解的事情。否则,我们会做一些我也不确定的事情吗?
有人会提供帮助和详细说明吗,这里到底发生了什么?