ASP.NET MVC 3 控制器 .Json 方法序列化不考虑 DataMember 名称属性

发布于 2024-11-07 05:58:05 字数 425 浏览 1 评论 0原文

在我的班级中,我得到了:

[DataMember(Name = "jsonMemberName", EmitDefaultValue = false, 
    IsRequired = false)]
public List<string> Member { get; set; }

通过重新运行 System.Web.Mvc.JsonResult 的控制器的 Json(obj) 传递对象后:我得到了序列化的 json: {Member:...} 但不是 {jsonMemberName:... },所以它不会查看 DataMember(Name = "jsonMemberName")。

如果我使用 System.Runtime.Serialization.Json 的序列化,一切都会按预期正常工作。

有什么问题吗?

In my class i've got:

[DataMember(Name = "jsonMemberName", EmitDefaultValue = false, 
    IsRequired = false)]
public List<string> Member { get; set; }

After passing the object through controller's Json(obj) that retruns System.Web.Mvc.JsonResult: i've got serialized json: {Member:...} but not {jsonMemberName:...}, so it doesn't look at DataMember(Name = "jsonMemberName").

If I use serialization from System.Runtime.Serialization.Json everithing's works fine as expected.

What can be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-11-14 05:58:05

您从控制器操作(使用 return Json(...))在内部依赖于 JavaScriptSerializer 类。此类不考虑模型上的任何 DataMember 属性。

您可以编写一个自定义 ActionResult,它使用 System.Runtime.Serialization.Json 命名空间中的序列化器。

例如:

public class MyJsonResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        if (!string.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = this.ContentEncoding;
        }
        if (Data != null)
        {
            var serializer = new DataContractJsonSerializer(Data.GetType());
            serializer.WriteObject(response.OutputStream, Data);
        }
    }
}

然后在您的控制器操作中:

public ActionResult Foo()
{
    var model = ...
    return new MyJsonResult { Data = model };
}

The JsonResult action which you are returning from the controller action (using return Json(...)) internally relies on the JavaScriptSerializer class. This class doesn't take into account any DataMember attributes on your model.

You could write a custom ActionResult which uses the serializer in the System.Runtime.Serialization.Json namespace.

For example:

public class MyJsonResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        if (!string.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = this.ContentEncoding;
        }
        if (Data != null)
        {
            var serializer = new DataContractJsonSerializer(Data.GetType());
            serializer.WriteObject(response.OutputStream, Data);
        }
    }
}

and then in your controller action:

public ActionResult Foo()
{
    var model = ...
    return new MyJsonResult { Data = model };
}
牛↙奶布丁 2024-11-14 05:58:05

System.Web.Mvc.JsonResult 使用旧的 JavaScriptSerializer 类,它不了解有关 DataAnnotations 程序集的任何信息。您需要使用 DataContractJsonSerializer

如果您愿意,可以在 JsonResult 上使用它:(

public class DataContractJsonResult : JsonResult
{
    public DataContractJsonResult(object data)
    {
        Data = data;
    }

    public DataContractJsonResult()
    {
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
            String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");
        }

        HttpResponseBase response = context.HttpContext.Response;

        if (!string.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }
        if (Data != null)
        {
            var serializer = new DataContractJsonSerializer(Data.GetType());
            var ms = new MemoryStream();
            serializer.WriteObject(ms, Data);
            string json = Encoding.UTF8.GetString(ms.ToArray());
            response.Write(json);
        }
    }
}

我参考了 ASP.NET MVC 源代码来创建它。不确定我是否必须以某种方式相信它。好吧,除了这个之外,就是这样。 :))

您还可以将其添加到控制器继承的基类中:

protected JsonResult DataContractJson(object data)
{
    return new DataContractJsonResult(data);
}

System.Web.Mvc.JsonResult usea the old JavaScriptSerializer class, which doesn't know anything about the DataAnnotiations assembly. You need to use the DataContractJsonSerializer instead.

You can use this instead on JsonResult if you so desire:

public class DataContractJsonResult : JsonResult
{
    public DataContractJsonResult(object data)
    {
        Data = data;
    }

    public DataContractJsonResult()
    {
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
            String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");
        }

        HttpResponseBase response = context.HttpContext.Response;

        if (!string.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }
        if (Data != null)
        {
            var serializer = new DataContractJsonSerializer(Data.GetType());
            var ms = new MemoryStream();
            serializer.WriteObject(ms, Data);
            string json = Encoding.UTF8.GetString(ms.ToArray());
            response.Write(json);
        }
    }
}

(I referenced the ASP.NET MVC source code to create this. Not sure if I have to credit it in some way. Well, more than this aside already is, that is. :))

You can also add this to a base class from which your controllers inherit:

protected JsonResult DataContractJson(object data)
{
    return new DataContractJsonResult(data);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文