ASP.NET Web API 跨域访问
自定义特性
要在WebApi中实现JSONP,一种方式是实现自定义特性 http://stackoverflow.com/questions/9421312/jsonp-with-asp-net-web-api/
public class JsonCallbackAttribute : ActionFilterAttribute
{
private const string CallbackQueryParameter = "callback"; public override void OnActionExecuted(HttpActionExecutedContext context)
{
var callback = string.Empty; if (IsJsonp(out callback))
{
var jsonBuilder = new StringBuilder(callback);
jsonBuilder.AppendFormat("({0})", context.Response.Content.ReadAsStringAsync().Result);
context.Response.Content = new StringContent(jsonBuilder.ToString());
}
base.OnActionExecuted(context);
} private bool IsJsonp(out string callback)
{
callback = HttpContext.Current.Request.QueryString[CallbackQueryParameter];
return !string.IsNullOrEmpty(callback);
}
}
后面只需要在需要支持JSONP的方法上加上JsonCallback特性即可。
自定义JsonMediaTypeFormatter
大A的文章:http://www.cnblogs.com/artech/p/cors-4-asp-net-web-api-03.html
WebApiContrib.Formatting.Jsonp
https://github.com/WebApiContrib/WebApiContrib.Formatting.Jsonp
Install-Package WebApiContrib.Formatting.Jsonp
修改Global.asax.cs文件
protected void Application_Start()
{
//add jsonp
GlobalConfiguration.Configuration.AddJsonpFormatter(); AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
需要支持jsonP的方法都加上callback参数即可
public IEnumerable<User> NextPage(int id, string callback = "")
官方的方式
http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api
Install-Package Microsoft.AspNet.WebApi.Cors
修改WebApiConfig.Register启用JSONP
config.EnableCors();
在Controller上加上Attribute
[EnableCors(origins: http://jsonpapi.xxxx.net, headers: "*", methods: "*")]
检查防火墙启用了 OPTIONS类型的请求 请求,这个问题找了我好久
REFER:
http://stackoverflow.com/questions/9421312/jsonp-with-asp-net-web-api
http://edi.wang/post/2013/12/27/tips-for-aspnet-webapi-cors
ASP.NET Web API 跨域访问的更多相关文章
- ASP.NET Web API 跨域访问(CORS)
一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...
- ASP.NET Web API 跨域访问(CORS)要注意的地方
一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...
- Web Api跨域访问配置及调用示例
1.Web Api跨域访问配置. 在Web.config中的system.webServer内添加以下代码: <httpProtocol> <customHeaders> &l ...
- asp.net web api 跨域问题
缘起 以前在asp.net mvc时代,很少出现跨域问题 自从使用了asp.net web api + angular (1/2)之后,开始有跨域问题了. 简单普及下跨域: 我的理解是只要是前台页面与 ...
- Web API 跨域访问(CORS)
1.在web.config里把“ <remove name="OPTIONSVerbHandler" /> ”删掉. 2. 到nuget上装一个包: ht ...
- asp.net web api 跨域,带cookie
官网上有一个介绍 http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api 但是只支 ...
- ASP.NET web api 跨域请求
1.学习文章:AJAX 跨域请求 - JSONP获取JSON数据 1.asp.net代码 参考文章:http://www.sxt.cn/info-2790-u-756.html (1).增加CorsH ...
- web api 跨域访问
在工程中 Install-Package Microsoft.AspNet.WebApi.Cors 在 webapiconfig.cs中 config.EnableCors(); 在 控制器中, [E ...
- Laravel API跨域访问的实现步骤
本篇文章给大家带来的内容是关于Laravel API跨域访问的实现步骤,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 服务器A请求服务器B的接口,那么一般会出现跨域问题. 1 XML ...
随机推荐
- linux 修改ssh端口号
vi /etc/ssh/sshd_config 找到#Port 22一段,这里是标识默认使用22端口,修改为如下: 代码如下 复制代码 Port 22 Port 50000 然后保存退出 执行/et ...
- HTTP的应用httpclient 和线程
package com.example.service; import java.io.IOException; import java.io.InputStream; import org.apac ...
- Python学习笔记(四)字符串型
字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 在最新的Python 3版本中,字符串是以Unicode编码的,也就是说,Python的字符串支持多语言 ...
- mybaits中xml文件大于号和小于号的处理方法
1.转义字符 原符号 < <= > >= & ' " 替换符号 < <= > >= & ' " 2 ...
- IOC和AOP使用扩展 多种方式实现依赖注入
多种方式实现依赖注入 1.Spring 使用setter访问器实现对属性的赋值, 2.Spring 构造constructor方法赋值, 3.接口注入 4.Spring P命名空间注入直接量 sett ...
- UI自动化测试的那些事
互联网产品的迭代速度远高于传统软件,尤其是移动APP不但更新频繁,还需要在不同硬件.系统版本的环境下进行大量兼容测试,这就给传统测试方法和测试工具带来了巨大挑战.为满足产品敏捷开发.快速迭代的需求,自 ...
- iOS模拟器设置输入中文
1.打开模拟器,选择Settings; 2.选择General ->KeyBoard ->KeyBoards,选择中文输入法Chinese(Simplified)即可 以前尝试试了很多方 ...
- deep learning
今天跑一个模型,程序都没变,就配置文件变了.但是总是很快就显示loss为nan. 检查配置文件还是不行,把其中loss改为0还是不行.最后搁置了一下,再回头对比一下电脑上的和服务器上的,发现一个配置文 ...
- shell脚本学习--shell中的变量$
$$ :Shell本身的PID(ProcessID) $! :Shell最后运行的后台Process的PID $? :最后运行的命令的结束代码(返回值) $- :使用Set命令设定的Flag一览 $* ...
- eclipse插件egit安装使用
转载http://blog.csdn.net/zhangdaiscott/article/details/16939165 安装问题解决: 1 Cannot complete the install ...