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 ...
随机推荐
- IDEA 用了maven后的 智能提示 不出现问题,项目的依赖包没有加载依赖库中的问题。
- 创建Android项目时出错——No resource found that matches the given name 'Theme.AppCompat.Light'
创建Android项目时出错,error: Error retrieving parent for item: No resource found that matches the given nam ...
- Python学习笔记(二)基本语法
Class 2 一.交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,如下图: ...
- 开源GIS简介.学习
开发者都希望自己的软件能够运行在尽可能多的计算机上.然而事与愿违,摆在 GIS开发者面前的仍然是对峙的平台.J2EE随着Java5.0的发布,已经正式更名为JavaEE,而微软也正式发布了.NET2. ...
- delete drop truncate 区别
truncate 删除内容,并释放空间,并不删除表结构,删除标识列,标识列重新从1开始delete 删除内容,不释放空间,不删除表结构,不删除标识列,标识列继续增加drop 直接删除表
- SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 解决办法
当跑jndi项目时抛出:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' fo ...
- python--自动删除文件
1.目的:定期自定删除7天前的数据 python脚本如下: #coding=utf-8 import os,time,datetime #需定时删除的目录的上一层路径 data_dir="/ ...
- linux学习之——学习路线(摘抄)
摘抄某笔者的Linux练习的道路图(rolistingmap): 对比一下为什么要学习linux 了解Linux的基础常识,这些包括了用户管理.群组的概念.权限的观念等 掌握至多50个以上的常用命令 ...
- 如何绑定android点击事件--跳转到另一个页面并实现关闭功能?
一.点击按钮跳转到另一个页面. eg:实现从一个页面点击跳转到另一个页面 1.首先在一个布局文件(.XML)中绘画了一个跳转按钮(id为btn1): <Button androi ...
- html添加网络音乐
IE浏览器,其他的不一定适应 <embed src="http://www.kmfhsj.com/fish-photo/music/xiaochenggushi.mp3" a ...