1.特性方式

 主要是继承ActionFilterAttribute,重写OnActionExecuted方法,在action执行后,给响应头加上一个键值对。

 using System.Web.Http.Filters;
public class OriginAttribute: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);
}
}

这里说下ActionFilterAttribute吧,其实转到定义就很明了的看到了,熟悉ASP.NET MVC的一眼看到的会感觉这不是和MVC中的一样么,其实只是函数个数一样,在MVC中定义的函数分别是OnActionExecuting、OnActionExecuted、OnResultExecuting、OnResultExecuted,区别就在这两个

webapi:

MVC:

用法没啥特殊,可以加在Controller上,也可以加载Action上

        [Origin]
public HttpResponseMessage GetProductsALL()
{
HttpResponseMessage rs = new HttpResponseMessage() { Content = new StringContent(JsonConvert.SerializeObject(products), System.Text.Encoding.UTF8, "application/json")};
return rs;
}

2.freamwork(V4.5+)自带的 System.Web.Http.Cors

先在Global.asax.cs文件中配置GlobalConfiguration,然后像加特性一样加载Controller或者Action上

  public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configuration.EnableCors();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
         [EnableCors(origins:"*",headers:"*",methods:"*")]
// [Origin]
public HttpResponseMessage GetProductsALL()
{
HttpResponseMessage rs = new HttpResponseMessage() { Content = new StringContent(JsonConvert.SerializeObject(products), System.Text.Encoding.UTF8, "application/json")};
return rs;
}

  orgins:Comma-separated list of origins that are allowed to access the resource. Use "*" to allow all

      允许访问资源的逗号分隔的源列表。 使用“*”允许所有

  headers:

  methods:

      Comma-separated list of headers that are supported by the resource. Use "*" to allow all. Use null or empty string to allow none

      以逗号分隔的资源支持的标题列表。 使用“*”允许所有。 使用null或空字符串不允许

System.Web.Http.Cors介绍的只是冰山一角,系列文章可以访问 http://www.cnblogs.com/artech/p/cors-4-asp-net-web-api-05.html

  

Asp.Net WebApi服务端解决跨域方案的更多相关文章

  1. asp.net core webapi 服务端配置跨域

    在前后端分离开发中服务端仅仅只为前端提供api接口,并且前后端往往单独部署,此时就会出现浏览器跨域问题.asp.net core提供了简单优雅的解决方案. 在startup文件的Configure添加 ...

  2. [django]django配合前端vue前后端联调,django服务端解决跨域(django-cors-headers)

    django内部csrf post提交数据解决 https://www.cnblogs.com/iiiiiher/articles/9164940.html 前端写了个页面,里面$.post发现403 ...

  3. asp.net (webapi) core 2.1 跨域配置

    原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...

  4. php服务端允许跨域访问

    >>php服务端允许跨域访问<< >>同源策略和跨域解决方案<<

  5. ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据的传递

    前言 最近公司项目进行架构调整,由原来的三层架构改进升级到微服务架构(准确的说是服务化,还没完全做到微的程度,颗粒度没那么细),遵循RESTFull规范,使前后端完全分离,实现大前端思想.由于是初次尝 ...

  6. php 服务端允许跨域访问

    加上需要允许跨域访问,配置如下(一下配置内容前不允许有其他任何输出操作): //设置允许跨域的 请求源地址//方式一:header("Access-Control-Allow-Origin: ...

  7. PHP服务端支持跨域

    跨域 由于浏览器的同源策略,导致浏览器页面访问非同源(协议.域名.端口任一不同)服务器产生跨域问题! PHP服务端配置支持跨域: // 指定允许其他域名访问, * 表示全部域名 header('Acc ...

  8. nodejs设置服务端允许跨域

    //设置跨域访问 app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", ...

  9. JSONP解决跨域方案

    一.jsonp原理 本质并不是ajax,只是执行了跨域js,所以该方式只支持get方式 html中,所有带src属性的标签都可以跨域script img iframe 所以,可以通过script加载其 ...

随机推荐

  1. F - 等式(1/x + 1/y = 1/n)

    链接:https://www.nowcoder.com/acm/contest/90/F来源:牛客网 题目描述 给定n,求1/x + 1/y = 1/n (x<=y)的解数.(x.y.n均为正整 ...

  2. Java的Hashtable类(转)

    文章来源:http://blog.csdn.net/zhna123_2011/article/details/6741479 ps:直接copy 哈希表是一种重要的存储方式,也是一种常见的检索方法.其 ...

  3. 洛谷——P1154 奶牛分厩

    P1154 奶牛分厩 题目描述 农夫约翰有N(1<=N<=5000)头奶牛,每头奶牛都有一个唯一的不同于其它奶牛的编号Si,所有的奶牛都睡在一个有K个厩的谷仓中,厩的编号为0到K-1.每头 ...

  4. 【单调队列】【动态规划】bzoj3831 [Poi2014]Little Bird

    f(i)=min{f(j)+(D(j)<=D(i))} (max(1,i-k)<=j<=i) 有两个变量,很难用单调队列,但是(引用): 如果fi<fj,i一定比j优秀.因为如 ...

  5. 【暴力】bzoj3713 [PA2014]Iloczyn

    没什么好说的. #include<cstdio> using namespace std; typedef long long ll; ]; int main() { scanf(]=; ...

  6. Spark小问题合集

    1)在win7下使用spark shell运行spark程序,通过以下形式读取文件时 sc.sequenceFile[Int,String]("./sparkF") 偶尔会出现“I ...

  7. Spark IDEA 调试(反编译)

    1)以WordCount为例,具体代码如下: import org.apache.spark.SparkConf import org.apache.spark.SparkContext; impor ...

  8. 微信小程序退款【证书的使用】

    1,官方文档的地址 2,在官方文档中给出了证书使用的链接,如下: [其实只有证书的获取,选择.具体的证书怎么在代码中使用,文档中并没有给出说明] 3,第一步准备请求的参数,里面只有五个是参数是有点特殊 ...

  9. 【InteillJ IDEA】Git的安装+同步项目到GitHub上

    需要的工具: 1.InteillJ IDEA 2.Git 3.GitHub帐号 步骤: 1.下载Git 下载地址:https://git-scm.com/downloads 安装完成后 勾选Launc ...

  10. 【js】判断浏览器是否IE浏览器

    搜罗各种方法来判断浏览器是否为IE浏览器 1.最简单的[来自:http://www.cnblogs.com/heganlin/p/5889743.html] if(!+[1,]){ layer.msg ...