Asp.Net WebApi服务端解决跨域方案
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服务端解决跨域方案的更多相关文章
- asp.net core webapi 服务端配置跨域
在前后端分离开发中服务端仅仅只为前端提供api接口,并且前后端往往单独部署,此时就会出现浏览器跨域问题.asp.net core提供了简单优雅的解决方案. 在startup文件的Configure添加 ...
- [django]django配合前端vue前后端联调,django服务端解决跨域(django-cors-headers)
django内部csrf post提交数据解决 https://www.cnblogs.com/iiiiiher/articles/9164940.html 前端写了个页面,里面$.post发现403 ...
- asp.net (webapi) core 2.1 跨域配置
原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...
- php服务端允许跨域访问
>>php服务端允许跨域访问<< >>同源策略和跨域解决方案<<
- ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据的传递
前言 最近公司项目进行架构调整,由原来的三层架构改进升级到微服务架构(准确的说是服务化,还没完全做到微的程度,颗粒度没那么细),遵循RESTFull规范,使前后端完全分离,实现大前端思想.由于是初次尝 ...
- php 服务端允许跨域访问
加上需要允许跨域访问,配置如下(一下配置内容前不允许有其他任何输出操作): //设置允许跨域的 请求源地址//方式一:header("Access-Control-Allow-Origin: ...
- PHP服务端支持跨域
跨域 由于浏览器的同源策略,导致浏览器页面访问非同源(协议.域名.端口任一不同)服务器产生跨域问题! PHP服务端配置支持跨域: // 指定允许其他域名访问, * 表示全部域名 header('Acc ...
- nodejs设置服务端允许跨域
//设置跨域访问 app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", ...
- JSONP解决跨域方案
一.jsonp原理 本质并不是ajax,只是执行了跨域js,所以该方式只支持get方式 html中,所有带src属性的标签都可以跨域script img iframe 所以,可以通过script加载其 ...
随机推荐
- 如何通过chrome的开发者工具查找新浪评论数据在哪个文件
1.打开开发者工具(ctrl+shift+i) 2.打开搜索(Esc) 示例:http://comment5.news.sina.com.cn/page/info?format=js&chan ...
- redhat 安装 setuptools【成功】
1. wget --no-check-certificate http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py ...
- 19、Flask实战第19天:CSRF攻击与防御
CSRF攻击原理 网站是通过cookie来实现登录功能的.而cookie只要存在浏览器中,那么浏览器在访问这个cookie的服务器的时候,就会自动的携带cookie信息到服务器上去.那么这时候就存在一 ...
- oracle to_char FM099999
to_char(column,'FM099999') The FM in the format removes leading and trailing blanks.
- 【暴力】Codeforces Round #398 (Div. 2) A. Snacktower
题意不复述. 用个bool数组记录一下,如果某一天,当前剩下的最大的出现了的话,就输出一段. #include<cstdio> using namespace std; int n; bo ...
- 【二分答案】【Heap-Dijkstra】bzoj2709 [Violet 1]迷宫花园
显然最短路长度随着v的变化是单调的,于是可以二分答案,据说spfa在网格图上表现较差. #include<cstdio> #include<cstring> #include& ...
- 【三维偏序】【分块】bzoj3262 陌上花开
裸的三维偏序. 对x坐标排序,y.z坐标分块.复杂度O(n*sqrt(n*log(n))).代码很短. #include<cstdio> #include<cmath> #in ...
- Scala实战高手****第7课:零基础实战Scala面向对象编程及Spark源码解析
/** * 如果有这些语法的支持,我们说这门语言是支持面向对象的语言 * 其实真正面向对象的精髓是不是封装.继承.多态呢? * --->肯定不是,封装.继承.多态,只不过是支撑面向对象的 * 一 ...
- @RequestMapping注解的使用,Controller方法返回值
1,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- Python自带的hmac模块
Python自带的hmac模块实现了标准的Hmac算法 我们首先需要准备待计算的原始消息message,随机key,哈希算法,这里采用MD5,使用hmac的代码如下: import hmac mess ...