我的项目是vue + ASP.NET 。在 Vue调试时,由于vue开启的调试用的服务器端口号 和 后台.NET程序的端口号是不同的,发送Ajax请求时,就会报错。这里就不提报错的原因了,网上有很多,基本的解决方法就是为对应的Action添加Header,比如以下代码:

        public ActionResult MeetingList() {

            Response.AppendHeader("Access-Control-Allow-Origin", "*");
Response.Headers.Add("Access-Control-Allow-Headers", "x-requested-with,content-type"); if (Request.HttpMethod.Equals("Options", StringComparison.OrdinalIgnoreCase))
{
return new EmptyResult();
} var meetingList = _meetingRepository.GetMeetings(); var MeetingDtoList = Mapper.Map<List<MeetingSelectDto>>(meetingList); return Json(MeetingDtoList, JsonRequestBehavior.AllowGet);
}

原理虽然如此,但是要为每一个Action都添加这样的代码,太不友好了。

这时可以使用Owin 的管道提前处理每一个请求,关于Owin,又是一个大话题,请自行google。

这里给一个自己写Owin中间件的教程链接 http://www.cnblogs.com/gaobing/p/5076089.html

我根据上面的链接,写的代码如下:

namespace WebApplication1.Middleware
{
public class CORSMiddleware : OwinMiddleware
{
public CORSMiddleware(OwinMiddleware next) : base(next) { } public override Task Invoke(IOwinContext context)
{ context.Response.Headers.Add("Access-Control-Allow-Origin",new string[] { "*" }); context.Response.Headers.Add("Access-Control-Allow-Headers", new string[] { "x-requested-with,content-type" }); if (context.Request.Method.Equals("Options", StringComparison.OrdinalIgnoreCase))
{
return Task.FromResult<int>();
}
return Next.Invoke(context);
}
}
}
namespace WebApplication1.Middleware
{
public static class AppBuilderExtension
{
public static IAppBuilder UseCORS(this IAppBuilder builder)
{
return builder.Use<CORSMiddleware>(); } }
}

之后在Startup里调用 UseCORS函数就可以了!

利用Owin解决CORS报错问题的更多相关文章

  1. Idea使用记录--添加Problems&&解决Autowired报错could not autowire

    今天在使用Idea的时候,发现Idea在整个每次找到错误代码非常不方便(Idea如果类中有错误,没有打开过类并不会提示,比如构建工程后缺少jar包问题).我想快速看到工程哪里出问题类似于eclipse ...

  2. [转]解决Maven报错"Plugin execution not covered by lifecycle configuration"

    [转]解决Maven报错"Plugin execution not covered by lifecycle configuration" 导入Myabtis源码后,POM文件会报 ...

  3. 解决MySQL报错ERROR 2002 (HY000)【转】

    今天在为新的业务线搭架数据库后,在启动的时候报错 root@qsbilldatahis-db01:/usr/local/mysql/bin# ./mysql ERROR 2002 (HY000): C ...

  4. vue解决启动报错cjs loader.js Error: Cannot find module '../config'问题

    vue解决启动报错cjs loader.js Error: Cannot find module '../config'问题 今天下载了一个开源项目一直运行不了,折腾了半天才找到问题所在,config ...

  5. 解决 php 报错 open_basedir restriction in effect或者nginx提示No input file specified怎么办

    解决 php 报错 open_basedir restriction in effect或者nginx提示No input file specified怎么办 问题是出现在了PHP.INI上面了 ,原 ...

  6. 解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat

    解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat ...

  7. eclips中maven解决jsp报错的问题

    加入如下的pom依赖: <!-- 解决jsp报错的依赖包第一个 --> <dependency> <groupId>javax.servlet</groupI ...

  8. 不修改系统日期和时间格式,解决Delphi报错提示 '****-**-**'is not a valid date and time

    假如操作系统的日期格式不是yyyy-MM-dd格式,而是用strtodate('2014-10-01')) 来转换的话,程序会提示爆粗 '****-**-**'is not a valid date ...

  9. Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).

    Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists). Git fet ...

随机推荐

  1. mysql-笔记 隔离级别、事务

    1 隔离级别:低级别的隔离通常可以执行更高的并发,系统 开销也更低 2 Read uncommitted:事务可以读取未提交的数据,脏读,应少用 3 read committed:不可重复读,事务只能 ...

  2. CTFcracktools——非常实用的CTF解密工具

    做bugku的crypto题时偶然发现了这个,吐血推荐!! 十分全面好用 整合了常见的解码.进制转换等CTF常用的工具: 下载地址: https://github.com/0Linchen/CTFCr ...

  3. 百度编辑器html网页显示

    $(function () { var ue = UE.getEditor('content',{ serverUrl:'{:\\think\\Url::build("Ueditor/ind ...

  4. BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块

    题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...

  5. Spring定时器配置与运用,及Cron表达式的详解

    一:首先在spring的配置文件里配置一个定时器 <task:executor id="executor" pool-size="5" /> < ...

  6. python图形用户

    1)使用GUI 1.GUI:Graphical user interface 2.tkinter:GUI libary for Python自带的库 3.GUI:Example 2)Ubuntu18. ...

  7. 主成分分析_PCA解释

    粘贴自:http://blog.codinglabs.org/articles/pca-tutorial.html 数据的向量表示及降维问题 向量的表示及基变换 协方差矩阵及优化目标 协方差矩阵对角化 ...

  8. vue+elementUI+axios实现的全局loading加载动画

    在项目中,很多时候都需要loading加载动画来缓解用户的焦虑等待,比如说,我打开了一个页面,而这个页面有很多接口请求,但浏览器的请求并发数就那么几个,再加上如果网速不行的话,那么这时候,用户很可能就 ...

  9. python13 1.函数的嵌套定义 2.global、nonlocal关键字 3.闭包及闭包的运用场景 4.装饰器

    ## 复习   '''1.函数对象:函数名 => 存放的是函数的内存地址1)函数名 - 找到的是函数的内存地址2)函数名() - 调用函数 => 函数的返回值  eg:fn()() =&g ...

  10. Java基础--面向对象编程4(多态)

    1.多态的概念 多态是指程序中的同一引用类型,使用不同的实例而执行结果不同的. 同一个类型:一般指父类 不同的实例:不同的子类实例 执行结果不同:针对同一方法执行的结果不同 package cn.sx ...