Resolving Webapi cross-domain issues using filter attribute tagging methods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.