Last active
September 16, 2016 17:43
-
-
Save frederikprijck/22b59541cdab32ac5fdda1a060621587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OLD CODE WITH INLINE REMARKS | |
public class TestController : Controller | |
{ | |
[HttpOptions] | |
public ActionResult Xlsx() | |
{ | |
// Catches and authorises pre-flight requests for /data/xlsx from remote domains | |
// Remark: Don't touch Response / Request object directly inside the Controller | |
Response.AddHeader("Access-Control-Allow-Origin", "*"); | |
Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
Response.AddHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS"); | |
return null; | |
} | |
[HttpPost] | |
public ActionResult Xlsx(string data, string name) | |
{ | |
Xlsx(); // Add CORS headers | |
/* ... implementation here ... */ | |
} | |
} | |
// Filter | |
public class CorsFilter : ActionFilterAttribute | |
{ | |
public override void OnActionExecuted(ActionExecutedContext filterContext) | |
{ | |
base.OnActionExecuted(filterContext); | |
var response = filterContext.RequestContext.HttpContext.Response; | |
// Add header to response | |
response.AddHeader("Access-Control-Allow-Origin", "*"); | |
response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
response.AddHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS"); | |
} | |
} | |
// New controller | |
public class Test2Controller : Controller | |
{ | |
[CorsFilter] | |
[HttpOptions] | |
public ActionResult Xlsx() | |
{ | |
return null; | |
} | |
[CorsFilter] | |
[HttpPost] | |
public ActionResult Xlsx(string data, string name) | |
{ | |
/* ... implementation here ... */ | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment