ASP.net MVC返回JSONP

问题描述 投票:72回答:6

我希望跨域返回一些JSON,并且我知道实现此目的的方法是通过JSONP而非纯JSON。我正在使用ASP.net MVC,因此我在考虑只扩展JSONResult类型,然后扩展ig Controller,以便它也实现了Jsonp方法。这是最好的解决方法,还是有内置的ActionResult可能更好?

编辑:我继续进行。仅供参考,我添加了一个新结果:

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
            }
        }
    }

还有我所有控制器超类的几个方法:

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
            };
        }

像魅力一样运作。

json asp.net-mvc jsonp
6个回答
16
投票

这是一个简单的解决方案,如果您不想定义一个动作过滤器

使用jQuery的客户端代码:

  $.ajax("http://www.myserver.com/Home/JsonpCall", { dataType: "jsonp" }).done(function (result) {});

MVC控制器动作。使用执行查询字符串提供的回调函数的JavaScript代码返回内容结果。同时设置JavaScript MIME类型以进行响应。

 public ContentResult JsonpCall(string callback)
 {
      return Content(String.Format("{0}({1});",
          callback, 
          new JavaScriptSerializer().Serialize(new { a = 1 })),    
          "application/javascript");
 }

13
投票

而不是使用Jsonp()方法对控制器进行子类化,我采用了扩展方法路线,因为它对我来说感觉更干净。关于JsonpResult的好处是,您可以像测试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。


10
投票

Ranju's blog post(又名“我找到的此博客文章”)非常好,阅读它可以使您进一步解决以下问题,以便您的控制器可以在同一控制器中优雅地处理同域JSON和跨域JSONP请求无需附加代码的操作[操作中]。

无论如何,对于“ give me the code”类型,这里是为了防止博客再次消失。

在您的控制器中(此代码段是新代码/非博客代码:]:

[AllowCrossSiteJson]
public ActionResult JsonpTime(string callback)
{
    string msg = DateTime.UtcNow.ToString("o");
    return new JsonpResult
    {
        Data = (new
        {
            time = msg
        })
    };
}

JsonpResult找到于this excellent blog post

/// <summary>
/// Renders result as JSON and also wraps the JSON in a call
/// to the callback function specified in "JsonpResult.Callback".
/// http://blogorama.nerdworks.in/entry-EnablingJSONPcallsonASPNETMVC.aspx
/// </summary>
public class JsonpResult : JsonResult
{
    /// <summary>
    /// Gets or sets the javascript callback function that is
    /// to be invoked in the resulting script output.
    /// </summary>
    /// <value>The callback function name.</value>
    public string Callback { get; set; }

    /// <summary>
    /// Enables processing of the result of an action method by a
    /// custom type that inherits from <see cref="T:System.Web.Mvc.ActionResult"/>.
    /// </summary>
    /// <param name="context">The context within which the
    /// result is executed.</param>
    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 (Callback == null || Callback.Length == 0)
            Callback = context.HttpContext.Request.QueryString["callback"];

        if (Data != null)
        {
            // The JavaScriptSerializer type was marked as obsolete
            // prior to .NET Framework 3.5 SP1 
#pragma warning disable 0618
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string ser = serializer.Serialize(Data);
            response.Write(Callback + "(" + ser + ");");
#pragma warning restore 0618
        }
    }
}

注:comments to the OP by @Ranju and others之后,我认为值得将Ranju的博客文章中的“最低限度”功能代码发布为社区Wiki。尽管可以肯定地说Ranju在他的博客上添加了上述代码和其他代码以供免费使用,但我不会在这里复制他的话。


0
投票

stimms和ranju v所引用的文章都非常有用,并且使情况清楚。

但是,我在使用扩展,在网上找到的MVC代码的上下文中进行子类化时不知所措。

有两个要点吸引我注意:

  1. 我从ActionResult派生的代码,但是在ExecuteResult中,有一些代码可以返回XML或JSON。
  2. 然后,我创建了一个基于泛型的ActionResult,以确保使用相同的ExecuteResults,而与我返回的数据类型无关。

因此,将两者结合在一起-我不需要进一步的扩展或子类来添加返回JSONP的机制,只需更改现有的ExecuteResults。

使我感到困惑的是,我确实在寻找一种方法来派生或扩展JsonResult,而无需重新编码ExecuteResult。由于JSONP实际上是带有前缀和后缀的JSON字符串,因此似乎很浪费。但是,最下面的ExecuteResult使用respone.write-因此,最安全的更改方式是重新编码ExecuteResults,就像各种帖子所提供的一样!

如果可以的话,我可以发布一些代码,但是此线程中已经有很多代码。


0
投票
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Template.Web.Helpers
{
    public class JsonpResult : JsonResult
    {
        public JsonpResult(string callbackName)
        {
            CallbackName = callbackName;
        }

        public JsonpResult()
            : this("jsoncallback")
        {
        }

        public string CallbackName { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var request = context.HttpContext.Request;
            var response = context.HttpContext.Response;

            string jsoncallback = ((context.RouteData.Values[CallbackName] as string) ?? request[CallbackName]) ?? CallbackName;

            if (!string.IsNullOrEmpty(jsoncallback))
            {
                if (string.IsNullOrEmpty(base.ContentType))
                {
                    base.ContentType = "application/x-javascript";
                }
                response.Write(string.Format("{0}(", jsoncallback));
            }

            base.ExecuteResult(context);

            if (!string.IsNullOrEmpty(jsoncallback))
            {
                response.Write(")");
            }
        }
    }

    public static class ControllerExtensions
    {
        public static JsonpResult Jsonp(this Controller controller, object data, string callbackName = "callback")
        {
            return new JsonpResult(callbackName)
            {
                Data = data,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        public static T DeserializeObject<T>(this Controller controller, string key) where T : class
        {
            var value = controller.HttpContext.Request.QueryString.Get(key);
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            return javaScriptSerializer.Deserialize<T>(value);
        }
    }
}

//Example of using the Jsonp function::
//  1-
public JsonResult Read()
{
    IEnumerable<User> result = context.All();        

    return this.Jsonp(result);
}

//2-
public JsonResult Update()
{
    var models = this.DeserializeObject<IEnumerable<User>>("models");
    if (models != null)
    {
        Update(models); //Update properties & save change in database
    }
    return this.Jsonp(models);
}

-2
投票

以上解决方案是一种很好的工作方式,但应使用新的结果类型扩展它,而不要使用返回JsonResult的方法,而应编写返回自己的结果类型的方法

public JsonPResult testMethod() {
    // use the other guys code to write a method that returns something
}

public class JsonPResult : JsonResult
{
    public FileUploadJsonResult(JsonResult data) {
        this.Data = data;
    }      

    public override void ExecuteResult(ControllerContext context)
    {
        this.ContentType = "text/html";
        context.HttpContext.Response.Write("<textarea>");
        base.ExecuteResult(context);
        context.HttpContext.Response.Write("</textarea>");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.