MVC及WebAPI添加Jsonp支持
Windows Live Writer 有点问题,着色代码看起来不清晰,所以贴的图片,完整代码在最后。
1:MVC实现
大致思路就是实现一个JsonpResult,在ExecuteResult内实现支持Jsonp,如图
实际上这样就可以在Action内返回JsonpResult对象就可以了。但为了方便起见,也可以添加个拓展。
这样一来我们就可以这样写
前台用ajax调用如下
2:WebApi实现
这里有两种用法,一种是自定义实现一个 JsonpMediaTypeFormatter,另外一种即为Filter
首先是JsonpMediaTypeFormatter,如下public class JsonpMediaTypeFormatter : JsonMediaTypeFormatter
{public string Callback { get; private set; }public JsonpMediaTypeFormatter(string callback = null)
{this.Callback = callback;
}public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{if (string.IsNullOrEmpty(this.Callback))
{return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}try{this.WriteToStream(type, value, writeStream, content);return Task.FromResult(new AsyncVoid());
}catch (Exception exception)
{
TaskCompletionSourcesource = new TaskCompletionSource();
source.SetException(exception);return source.Task;
}
}private void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
{
JsonSerializer serializer = JsonSerializer.Create(this.SerializerSettings);using (StreamWriter streamWriter = new StreamWriter(writeStream, this.SupportedEncodings.First()))using (JsonTextWriter jsonTextWriter = new JsonTextWriter(streamWriter) { CloseOutput = false })
{
jsonTextWriter.WriteRaw(this.Callback + "(");
serializer.Serialize(jsonTextWriter, value);
jsonTextWriter.WriteRaw(")");
}
}public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType)
{if (request.Method != HttpMethod.Get)
{return this;
}string callback;if (request.GetQueryNameValuePairs().ToDictionary(pair => pair.Key,
pair => pair.Value).TryGetValue("callback", out callback))
{return new JsonpMediaTypeFormatter(callback);
}return this;
}
[StructLayout(LayoutKind.Sequential, Size = 1)]private struct AsyncVoid
{ }
}
在Global.asax内注册
Controller
此时前端调用任何方法时,只要参数内包含callback即可使用Jsonp调用
然后是Filter方式,实现一个继承自 ActionFilterAttribute 的过滤器JsonpCallbackAttribute
在需要支持Jsonp的方法上加上该Attribute即可
前端调用同上。
3:效果及完整代码
MVC WebApi 发布到IIS上后不可以访问,本地测试是好的(跨域的)
除了ie内核的浏览器 火狐谷歌默认情况下是不支持跨域访问的
WEB api 与WEB MVC不同
在新出的MVC中,增加了WebAPI,用于提供REST风格的WebService,新生成的WebAPI项目和典型的MVC项目一样,包含主要的Models、Views、Controllers等文件夹和Global.asax文件。Views对于WebAPI来说没有太大的用途,Models中的Model主要用于保存Service和Client交互的对象,这些对象默认情况下会被转换为Json格式的数据迚行传输,Controllers中的Controller对应于WebService来说是一个Resource,用于提供服务。和普通的MVC一样,Global.asax用于配置路由规则。
对于WebAPI来说它最初被设计为和WCF一样的客户端、服务端两套结构我们到现在乊所以还没有提到客户端是因为我们的请求别的方式来封装成HTTP请求戒接收HTTP相应的比如AJAX和Form表单提交。