Mvcapi解决H5请求接口跨域问题
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Filters;
namespace MBT.WebKit.Filter
{
/// <summary>
/// 跨域过滤器
/// author:cza
/// date:2018-08-20
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class OriginWebAttribute : Attribute //: ActionFilterAttribute
{
//存储key的,未用到
public string AttributeKey = string.Empty;
public OriginWebAttribute()
{
}
/// <summary>
/// 跨域返回消息头的判断
/// author:cza
/// date:2018-08-20
/// </summary>
/// <param name="actionContext"></param>
public void OriginActionExecuted(System.Web.Http.Controllers.HttpActionContext actionContext) //System.Web.Http.Controllers.HttpControllerContext ControllerContext
{
System.Web.Http.Routing.IHttpRouteData data = actionContext.ControllerContext.RouteData;
//针对controller
var allowAnonymous = actionContext.ControllerContext.ControllerDescriptor.ControllerType.CustomAttributes.SingleOrDefault(m => m.AttributeType.Name == typeof(OriginWebAttribute).Name);
if (allowAnonymous == null)
{
//针对某个方法
Type controllerType = actionContext.ControllerContext.ControllerDescriptor.ControllerType.Assembly.GetTypes().Where(m => m.Name.ToLower() == data.Values["Controller"].ToString().ToLower() + "controller").FirstOrDefault();
var attributes = controllerType.GetMethods().Where(m => m.Name.ToLower() == data.Values["Action"].ToString().ToLower()).FirstOrDefault();
var allowAnonymousMethods = attributes.CustomAttributes.SingleOrDefault(m => m.AttributeType.Name == typeof(OriginWebAttribute).Name);
if (allowAnonymousMethods == null)
{
return;
}
}
#region 跨域的解决办法增加返回消息头
System.Uri uriReferrer = actionContext.ControllerContext.Request.Headers.Referrer;
if (uriReferrer != null)
{
string Origin = uriReferrer.Scheme + "://" + uriReferrer.Host;
int Hostport = uriReferrer.Port;
if (Hostport != 80 && Hostport != 443)
{
Origin = Origin + ":" + Hostport;
}
//没有返回Response消息头的话,不要返回,因为reponse为null的话,证明异常,会走全局捕获。
if (actionContext.Response != null)
{
//var package = PackageKit.GetResponsePackage<string>(null, 301, "");
//string json = JsonConvert.SerializeObject(package);
//actionContext.Response = new System.Net.Http.HttpResponseMessage();
//StringContent Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json");
//actionContext.Response.StatusCode = HttpStatusCode.OK;
//actionContext.Response.Content = Content;
//授权的请求域名
actionContext.Response.Headers.Add("Access-Control-Allow-Origin", Origin);
actionContext.Response.Headers.Add("Access-Control-Allow-Methods", "*"); //GET,POST,PUT,DELETE,OPTIONS
actionContext.Response.Headers.Add("Access-Control-Allow-Headers", "x-requested-with");
actionContext.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
}
}
#endregion
}
}
}
然后在ApiController或对应的方法加上这个自定义属性就可以了
Mvcapi解决H5请求接口跨域问题的更多相关文章
- axios解决调用后端接口跨域问题
vue-cli通过是本地代理的方式解决接口跨域问题的.但是在vue-cli的默认项目配置中这个代理是没有配置的,如果现在项目中使用,必须手动配置config/index.js文件 ... proxyT ...
- 解决ajax请求cors跨域问题
”已阻止跨源请求:同源策略禁止读取位于 ***** 的远程资源.(原因:CORS 头缺少 'Access-Control-Allow-Origin').“ ”已阻止跨源请求:同源策略禁止读取位于 ** ...
- jQuery解决ajax请求的跨域问题
这两天工作中频繁的遇到JS的跨域问题,都通过绕开ajax请求的方式.特地百度了一下,把跨域问题解决了.在这分析一下 首先贴上js的页面代码: <html> <head> < ...
- 【Vue+Node】解决axois请求数据跨域问题
项目基于Vue前端+Node后台,启动两个服务,请求数据时,端口不一致造成跨域报错: (No 'Access-Control-Allow-Origin' header is present on th ...
- JAVA联调接口跨域解决办法
JAVA联调接口跨域解决办法 第一种代码: HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus. ...
- Vue的学习总结之---Vue项目 前后端分离模式解决开发环境的跨域问题
原文:https://blog.csdn.net/localhost_1314/article/details/83623526 在前后端分离的web开发中,我们与后台联调时,会遇到跨域的问题. 比如 ...
- odoo接口跨域问题
odoo Controller接口开发 POST请求的跨域问题解决方法 1.odoo Controller接口开发,前端在请求的时候会发生跨域问题,报错信息如下:Function declared a ...
- 前端总结·基础篇·JS(四)异步请求及跨域方案
前端总结系列 前端总结·基础篇·CSS(一)布局 前端总结·基础篇·CSS(二)视觉 前端总结·基础篇·CSS(三)补充 前端总结·基础篇·JS(一)原型.原型链.构造函数和字符串(String) 前 ...
- Vue-cli proxyTable 解决开发环境的跨域问题
Vue-cli proxyTable 解决开发环境的跨域问题 proxyTable: { '/list': { target: 'http://api.xxxxxxxx.com', pathRewri ...
随机推荐
- .NET性能优化-你应该为集合类型设置初始大小
前言 计划开一个新的系列,来讲一讲在工作中经常用到的性能优化手段.思路和如何发现性能瓶颈,后续有时间的话应该会整理一系列的博文出来. 今天要谈的一个性能优化的Tips是一个老生常谈的点,但是也是很多人 ...
- plicp 点云迭代最近邻点配准法
输入参数 点云A的极坐标集合 点云A对应Lidar所在pose 点云B的极坐标集合 点云B对应Lidar所在pose Features 根据两个点云的弧度关系确定找点的起始位置 根据两个点云的弧度关系 ...
- 五二不休息,今天也学习,从JS执行栈角度图解递归以及二叉树的前、中、后遍历的底层差异
壹 ❀ 引 想必凡是接触过二叉树算法的同学,在刚上手那会,一定都经历过题目无从下手,甚至连题解都看不懂的痛苦.由于leetcode不方便调试,题目做错了也不知道错在哪里,最后无奈的cv答案后心里还不断 ...
- 【翻译】ScyllaDB数据建模的最佳实践
文章翻译自Scylla官方文档:https://www.scylladb.com/2019/08/20/best-practices-for-data-modeling/ 转载请注明出处:https: ...
- ajax 请求登录超时跳转登录页解决方法
在Filter里判断是否登录,如果未登录返回401状态 public class SelfOnlyAttribute : ActionFilterAttribute { public override ...
- 使用 VS Code + Markdown 编写 PDF 文档
背景介绍 作为一个技术人员,基本都需要编写技术相关文档,而且大部分技术人员都应该掌握 markdown 这个技能,使用 markdown 来编写并生成 PDF 文档将会是一个不错的体验,以下就介绍下如 ...
- 项目:PMBok
美国项目管理协会(PMI)资格认证之所以能在如此广的行业和地域范围内被迅速认可,首先是项目管理本身的重要性和实用性决定的,其次很大程度上是得益于该项认证体系本身的科学性.PMI早在七十年代末就率先提出 ...
- 自学c语言
C 语言是一种通用的.面向过程式的计算机程序设计语言. 当前最新的 C 语言标准为 C18 前期准备 C 编译器 写在源文件中的源代码是人类可读的源.它需要"编译",转为机器语言 ...
- Pandas:添加修改、高级过滤
1.添加修改数据 Pandas 的数据修改是进行赋值,先把要修改的数据筛选出来,然后将同结构或者可解包的数据赋值给它: 修改数值 df.Q1 = [1, 3, 5, 7, 9] * 20 # 就会把值 ...
- 开源项目 PM 浅谈如何设计官网
有用户反馈进入官网首页光秃秃的一片,大家忙着做产品,忽略了官网的建设,惭愧惭愧. 确实,极简风格和极其简单还是有很大区别的. 旧的 Web 端 除了极其简单之外,它还有一个小问题, ...