自定义特性

要在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 跨域访问的更多相关文章

  1. ASP.NET Web API 跨域访问(CORS)

    一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...

  2. ASP.NET Web API 跨域访问(CORS)要注意的地方

    一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...

  3. Web Api跨域访问配置及调用示例

    1.Web Api跨域访问配置. 在Web.config中的system.webServer内添加以下代码: <httpProtocol> <customHeaders> &l ...

  4. asp.net web api 跨域问题

    缘起 以前在asp.net mvc时代,很少出现跨域问题 自从使用了asp.net web api + angular (1/2)之后,开始有跨域问题了. 简单普及下跨域: 我的理解是只要是前台页面与 ...

  5. Web API 跨域访问(CORS)

    1.在web.config里把“    <remove name="OPTIONSVerbHandler" />  ”删掉. 2. 到nuget上装一个包:    ht ...

  6. asp.net web api 跨域,带cookie

    官网上有一个介绍 http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api 但是只支 ...

  7. ASP.NET web api 跨域请求

    1.学习文章:AJAX 跨域请求 - JSONP获取JSON数据 1.asp.net代码 参考文章:http://www.sxt.cn/info-2790-u-756.html (1).增加CorsH ...

  8. web api 跨域访问

    在工程中 Install-Package Microsoft.AspNet.WebApi.Cors 在 webapiconfig.cs中 config.EnableCors(); 在 控制器中, [E ...

  9. Laravel API跨域访问的实现步骤

    本篇文章给大家带来的内容是关于Laravel API跨域访问的实现步骤,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 服务器A请求服务器B的接口,那么一般会出现跨域问题. 1 XML ...

随机推荐

  1. const int * p 和 int const * p 和 int * const p 的区别

    首先注意,const int * p 和int const *p 是一样的,并且不管是不是*p,即使const int i和int const i也是一样的,所以我们接下来只讨论int const * ...

  2. winform.布局

    布局:默认布局:自己拖动进行布局,工具栏里对齐方式 右键,锁定.##随容器拉动变化属性:Anchor:上下左右,固定的设置 panel的排列 1.Dock属性:(顺序填充)Top:靠上,高度不变,左右 ...

  3. CentOS 问题集锦

    在CentOS 6更新后,不可避免的会在启动选项中产生多个内核选项,一个内核文件大概占100兆左右(一般100M以下),可以使用以下命令进行删除多余的内核. 1.首先列出系统中正在使用的内核: # u ...

  4. mysql小误区关于set global sql_slave_skip_counter=N命令

      背景知识1:     在主从库维护中,有时候需要跳过某个无法执行的命令,需要在slave处于stop状态下,执行 set global sql_slave_skip_counter=N以跳过命令. ...

  5. avalon2学习教程12数据验证

    avalon2砍掉了不少功能(如ms-include,ms-data),腾出空间加了其他更有用的功能.数据验证就是其中之一.现在avalon2内置的验证指令是参考之前的oniui验证框架与jquery ...

  6. CLR VIA C#委托

    1.什么是委托?委托就是一种回调函数的机制,将函数作为一个参数传递给其他对象,当该对象需要的时候调用委托来达到回调函数的目的. 通俗点的说法是:你将一件事情交给别人去做.例如你QQ里的自动回复,为了第 ...

  7. Ubuntu修改hosts方法

    1.修改hostssudo gedit /etc/hosts如果不喜欢使用gedit命令,而且当前帐户为非root帐户,那么可把/etc/hosts复制到桌面上,然后手动编辑后保存,再使用命令copy ...

  8. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  9. HQL

    以下内容全部摘自韩顺平老师Hibernate笔记 * uniqueResult方法 如果我们检索一个对象,明确知道最多只有一个对象,则建议使用该方法: 具体用法如下: Student s=(Stude ...

  10. c# SqlHelper Class

    using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...