为Asp.net WebApi 添加跨域支持
Nuget安装包:microsoft.aspnet.webapi.cors
原文地址:https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api
Enable CORS
Now let's enable CORS in the WebService app. First, add the CORS NuGet package. In Visual Studio, from the Tools menu, select Library Package Manager, then select Package Manager Console. In the Package Manager Console window, type the following command:
Install-Package Microsoft.AspNet.WebApi.Cors
This command installs the latest package and updates all dependencies, including the core Web API libraries. User the -Version flag to target a specific version. The CORS package requires Web API 2.0 or later.
Open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method.
using System.Web.Http;
namespace WebService
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// New code
config.EnableCors(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Next, add the [EnableCors] attribute to the TestController class:
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors; namespace WebService.Controllers
{
[EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]
public class TestController : ApiController
{
// Controller methods not shown...
}
}
For the origins parameter, use the URI where you deployed the WebClient application. This allows cross-origin requests from WebClient, while still disallowing all other cross-domain requests. Later, I’ll describe the parameters for [EnableCors] in more detail.
为Asp.net WebApi 添加跨域支持的更多相关文章
- 简析ASP.NET WebApi的跨域签名
之前的文章写了关于WebApi的跨域问题,当中的方法只是解决了简单请求的跨域问题而非简单请求的跨域问题则没有解决. 要弄清楚 CORS规范将哪些类型的跨域资源请求划分为简单请求的范畴,需要额外了解几个 ...
- webapi 开启跨域支持
1.Global文件: GlobalConfiguration.Configuration.EnableCors(); 2.需要跨域的action或controller添加跨域规则 [EnableCo ...
- Chrome 浏览器添加跨域支持
开发前端本地项目时,涉及到与后端服务器的通信联调,在使用 ajax 时由于浏览器的安全策略不允许跨域.一种方式是本地搭建转发服务器,今天又 GET 到一种更直接的方式,在 Chrome 浏览器开启时添 ...
- ASP.NET WebAPI解决跨域问题
跨域是个很蛋疼的问题...随笔记录一下... 一.安装nuget包:Microsoft.AspNet.WebApi.Core 二.在Application_Start方法中启用跨域 1 protect ...
- WebAPI+Html跨域时对session的支持
1.Global.asax中添加对Session的支持,重新Init方法: public override void Init() { this.PostAuthenticateRequest += ...
- asp.net core webapi之跨域(Cors)访问
这里说的跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被当作 ...
- 关于WebService、WebApi的跨域问题
随着移动互联网的发展, 传统营销模式往网站以及移动客户端转移已经成为一种趋势.接触过互联网开发的开发者肯定会很熟悉两种网络服务WebApi.WebService.在使用JavaScript进行数据交互 ...
- 浅谈Web Api配合SignalR的跨域支持
最近接手的一个项目中,涉及到一个简单的消息模块,由于之前有简单了解过SignalR,所以打算尝试着摸索摸索~! 首先,通过Nuget管理器添加Microsoft ASP.NET SignalR引用~目 ...
- C# WebAPI设置跨域
设置前端跨域请求很简单,只需要两个步骤 1.安装package Install-Package Microsoft.AspNet.WebApi.Cors 2.WebApiConfig类中,Regist ...
随机推荐
- UE4 WCF RestFul 服务器 读取JSON 数据并解析 简单实例
Note:不知道为什么通过Txt读取的JsonString,如果TXT 不是ANSI编码的话,会报JsonArrayStringToUStruct Unable to parse. bool UWg ...
- MAC按键以及快捷键
使用普通的非Mac自带的键盘的同志们,想要在Mini Mac上面想要使用键盘,则推荐使用Mac系统自带的虚拟键盘,这样就可以查看普通键盘上每个键对应的Mac系统上是什么. 查看Mac系统上的虚拟键盘的 ...
- 全国大学列表文件(较新)+ nodejs导入mongodb数据库
直接上代码 'use strict' var fs=require('fs'), mongodb=require('mongodb').MongoClient, assert=require('ass ...
- 实时控制软件设计 第二次作业 myRobot
#include<iostream> #include <Eigen/Dense> #include <math.h> #include <vector> ...
- 字符串数组转为PHP级数组
先要把字符串处理一下,成为php定义数组的形式,再用eval执行: $str="Array([15] => Array([id] => 2304[fromtype] => ...
- svn客户端命令
记几个常用的命令. 首次拉仓库时,先要进行检出(url可以带端口号): svn checkout http://svn.example.com:9834/repos svn checkout file ...
- 两个不同的list随机组合到一个List中。
今天组长给了一个绑定任务,业务需要把一男一女随机的老师绑定到考场. 测试例子入下: package com.test; import java.util.ArrayList; import java. ...
- cache缓存帮助类
public class CacheHelper { /// <summary> /// 创建缓存项的文件 /// </summary> /// <param name= ...
- Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- linux文件目录权限详解(20170101)
linux目录权限与文件权限是不同的,二者要相互配合,这是基础. 比如要读文件:目录至少要有x,文件至少要有r. 要写文件:目录至少要有x,文件至少要有rw. 要执行文件:目录至少要有x,文件至少要有 ...