I recently read this code that makes an MVC Web API allow CORS (Cross origing resource sharing). I understand that the ActionFilterAtrribute
makes this a filter, but I'm not sure what's going on in this class: 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);
}
}
}
So basically, if the request method we receive is a HttpOPTIONS
we do something which I don't quite understand in this case. Otherwise, we do something else that I'm not sure about either?
Would someone be helpful and elaborate, what's actually going on here?