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. ubuntu fcitx google 输入法打不出中括号【】

    编辑/usr/share/fcitx/data/punc.mb.zh_CN, 将 [ · ] 「 」 这部分改成自己习惯的: [  [ ]  ] 保存后,重启一下fcitx就OK了.

  2. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

  3. python编码问题 与 代码换行问题

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ python程序对于unicode码的支持情况不同 python3 支持较好,在文件开头加入如下 ...

  4. sed 概述

    sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送 ...

  5. HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. 关于scanf函数的返回值问题

    如: scanf("%d%d",&a,&b); 1.如果a和b都被成功读入,则scanf的返回值为2 2.如果只有a被成功读入,那么返回值为1 3.如果a和b都未被 ...

  7. ACM--素数距离问题

    题目描述:现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数,则输出左侧的值及相应距离.如果输入的整数本身就是素数,则输出该素数本身,距离输出 ...

  8. Android 中 Environment.getExternalStorageDirectory()无效

    我们在处理缓存的时候,并不是每次都会在应用私有存储空间那里保存,很多时候是需要用到ExternalStorage.我们平时一般都是用Environment.getExternalStorageDire ...

  9. JVM OQL查询语言

    OQL查询语言 SELECT Clause The SELECT clause determines what to extract from the heap dump. To display ob ...

  10. Android--使用XMLPull解析xml

    在Android中极力推荐的xmlpull方式解析xml.xmlpull不只能够使用在Android上.相同也适用于javase,但在javase环境下.你须要自己去获取xmlpull所依赖的类库. ...