开发者

ASP.NET MVC 3 controller .Json method serialization doesn't look at DataMember Name attribure

开发者 https://www.devze.com 2023-03-06 01:59 出处:网络
In my class i\'ve got: [DataMember(Name = \"jsonMemberName\", EmitDefaultValue = false, IsRequired = false)]

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.Json开发者_开发问答Result: 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?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消