In my previous essays.
Is it possible to use features to solve cross-domain problems, the answer is yes, yes, the operation is as follows:
First build a class Crosssiteattribute: an attribute class inherited from an abstract ActionFilterAttribute class to create an action filter or response filter. Then override the method onactionexecuted implement the cross-domain
Using System.web.http.filters;namespace api{public class Crosssiteattribute:actionfilterattribute { Private Const string origin = "origin"; Private Const string Accesscontrolalloworigin = "Access-control-allow-origin"; Private Const string Originheaderdefault = "*"; public override void OnActionExecuted (Httpactionexecutedcontext actionexecutedcontext) { ACTIONEXECUTEDCONTEXT.RESPONSE.HEADERS.ADD (Accesscontrolalloworigin, Originheaderdefault);}}
Finally, add features to your Webapi method to
[Crosssite] [HttpPost] publicstring Add () { return "1""2"; }
It is also possible to solve cross-domain problems by using the above method
Note:
Called by the ASP. OnActionExecuted after executing the action method.
Called by the ASP. OnActionExecuting before executing the action method.
Called by the ASP. Onresultexecuted after executing the result of the operation.
Onresultexecuting is called by the ASP. NET before executing the result of the operation.
Reference link: https://msdn.microsoft.com/zh-cn/library/system.web.mvc.actionfilterattribute (v=vs.108). aspx
Resolving Webapi cross-domain issues using filter attribute tagging methods