ASP.net MVC返回JSONP [英] ASP.net MVC returning JSONP

查看:309
本文介绍了ASP.net MVC返回JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待回到跨域一些JSON和我的理解是这样做的方式是通过JSONP,而不是纯粹的JSON。我使用ASP.net MVC所以我想只是延长JSONResult类型,然后extendig控制器,使其还实施了JSONP方法。这是为了去了解它的最好方法还是有一个内置的ActionResult这可能是更好的?

I am looking to return some JSON across domains and I understand that the way to do this is through JSONP rather than pure JSON. I am using ASP.net MVC so I was thinking about just extending the JSONResult type and then extendig Controller so that it also implemented a Jsonp method. Is this the best way to go about it or is there a built in ActionResult which might be better?

编辑:我继续这样做。仅供参考起见,我增加了一个新的结果:

I went ahead and did that. Just for reference sake I added a new result:

public class JsonpResult : System.Web.Mvc.JsonResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            HttpResponseBase response = context.HttpContext.Response;

            if (!String.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType = "application/javascript";
            }
            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null)
            {
                // The JavaScriptSerializer type was marked as obsolete prior to .NET Framework 3.5 SP1
#pragma warning disable 0618
                HttpRequestBase request = context.HttpContext.Request;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                response.Write(request.Params["jsoncallback"] + "(" + serializer.Serialize(Data) + ")");
#pragma warning restore 0618
            }
        }
    }

和还了几个方法来我的所有控制器的父类:

and also a couple of methods to a superclass of all my controllers:

protected internal JsonpResult Jsonp(object data)
        {
            return Jsonp(data, null /* contentType */);
        }

        protected internal JsonpResult Jsonp(object data, string contentType)
        {
            return Jsonp(data, contentType, null);
        }

        protected internal virtual JsonpResult Jsonp(object data, string contentType, Encoding contentEncoding)
        {
            return new JsonpResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };
        }

工程就像一个魅力。

Works like a charm.

推荐答案

而不是继承我的JSONP控制器()方法,我去扩展方法途径,因为它感觉触摸清洁给我。关于JSON的$​​ P $ psult的好处是,你可以测试它完全一样的方式将一个JsonResult。

Rather than subclassing my controllers with Jsonp() methods, I went the extension method route as it feels a touch cleaner to me. The nice thing about the JsonpResult is that you can test it exactly the same way you would a JsonResult.

我所做的:

public static class JsonResultExtensions
{
    public static JsonpResult ToJsonp(this JsonResult json)
    {
        return new JsonpResult { ContentEncoding = json.ContentEncoding, ContentType = json.ContentType, Data = json.Data, JsonRequestBehavior = json.JsonRequestBehavior};
    }
}

这样,您就不必担心创建所有不同的JSONP()重载,只是你JsonResult转换为JSONP之一。

This way you don't have to worry about creating all the different Jsonp() overloads, just convert your JsonResult to a Jsonp one.

这篇关于ASP.net MVC返回JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆