Asp.net mvc 输出与Razor视图中ApicController中相同的JSON

Asp.net mvc 输出与Razor视图中ApicController中相同的JSON,asp.net-mvc,razor,asp.net-web-api,Asp.net Mvc,Razor,Asp.net Web Api,我试图在Razor视图中直接输出JSON。正在序列化的对象是IEnumerable(请参见下面的定义)。我想将其序列化为相同的JSON,该JSON将从ApicController返回。以下是我尝试过的: 当使用@Json.Encode(theValue)时,它会忽略数据成员,因此属性会得到错误的名称(大写首字母) 当使用DataContractJsonSerializer(并将IEnumerable强制转换为IList)时,它将ImageDto.Sizes输出为[{Key:'foo',Valu

我试图在Razor视图中直接输出JSON。正在序列化的对象是
IEnumerable
(请参见下面的定义)。我想将其序列化为相同的JSON,该JSON将从
ApicController
返回。以下是我尝试过的:

  • 当使用
    @Json.Encode(theValue)
    时,它会忽略
    数据成员
    ,因此属性会得到错误的名称(大写首字母)
  • 当使用
    DataContractJsonSerializer
    (并将
    IEnumerable
    强制转换为
    IList
    )时,它将
    ImageDto.Sizes输出为
    [{Key:'foo',Value:}]
    ,但ApiController正确地执行
    {foo:}
以下是我的课程:

    [DataContract]
    public class ItemDto
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "title")]
        public string Title { get; set; }

        [DataMember(Name = "image")]
        public ImageDto Image { get; set; }

        [DataMember(Name = "price")]
        public decimal Price { get; set; }
    }

    [DataContract]
    public class ImageDto
    {
        [DataMember(Name = "id")]
        public Guid Id { get; set; }
        [DataMember(Name = "sizes")]
        public Dictionary<String, ImageSizeDto> Sizes { get; set; }
    }

    [DataContract]
    public class ImageSizeDto
    {
        [DataMember(Name = "url")]
        public string Url { get; set; }
        [DataMember(Name = "w")]
        public int Width { get; set; }
        [DataMember(Name = "h")]
        public int Height { get; set; }
    }
您可以使用:


请注意,在ASP.NET MVC4RTM中,Json.NET将是最新版本。在此之前,只需将内置序列化程序与Json.NET交换即可,如Scott Hanselman的博文所示。

谢谢!直到现在我才知道Json.Net有多棒…:)@Darin,关于MVC4RTM将使用JSON.NET,你有什么参考资料吗?我发现很多人说WebAPI使用它(并且可以确认在RC中就是这样),但MVC4似乎仍然使用DataContractSerializer。我不希望在RC+RTM之间发生如此重大的变化。@Richard,你没读过我的答案吗?其中有一个指向我提供的此引用的链接:。我引用Hanselman的话,以防您还没有读过它:
我们的web团队将在web API发布时将JSON.NET作为默认的JSON序列化程序,这很好。
包含在您的报价中:“在web API中”。在你的回答中,你说这将是MVC的默认设置。虽然Web API构成了ASP.NET MVC4的一部分,但它们不是一回事。MVC4RC中的视图页面中的Json.Encode使用DataContractSerializer。Web API使用JSON.NETAh如果您误解了我的答案,请道歉。Json.NET将是Web API的默认序列化程序。不知道ASP.NET MVC是什么。对我来说,它是早期的默认序列化程序,所以我并不在乎它是否是默认的:-)如果它是默认的,我所要做的就是删除我的自定义扩展方法,这些方法目前正在用Json.NET替换JavaScriptSerializer。如果不是,我就把它们留着。
[{"id":6,
 "title":"Foo bar baz",
 "image":
    {"id":"fb2a3b4a-5ae5-4d9d-baff-72e107aa6e9c",
     "sizes":
        {"Original": {"url":"http://example.com/a", "w":-1, "h":-1},
         "Thumbnail": {"url":"http://example.com/b", "w":130, "h":73},
         "LargeThumbnail": {"url":"http://example.com/c", "w":220,"h":124},
         "Popup": {"url":"http://example.com/d", "w":256, "h":256},
         "FullWidth": {"url":"http://example.com/e", "w":930, "h":524}}},
 "price":79}]
[DataContract]
public class ItemDto
{
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "title")]
    public string Title { get; set; }

    [DataMember(Name = "image")]
    public ImageDto Image { get; set; }

    [DataMember(Name = "price")]
    public decimal Price { get; set; }
}

[DataContract]
public class ImageDto
{
    [DataMember(Name = "id")]
    public Guid Id { get; set; }
    [DataMember(Name = "sizes")]
    public Dictionary<String, ImageSizeDto> Sizes { get; set; }
}

[DataContract]
public class ImageSizeDto
{
    [DataMember(Name = "url")]
    public string Url { get; set; }
    [DataMember(Name = "w")]
    public int Width { get; set; }
    [DataMember(Name = "h")]
    public int Height { get; set; }
}

class Program
{
    static void Main()
    {
        var items = new[] 
        {
            new ItemDto
            {
                Id = 6,
                Title = "Foo bar baz",
                Image = new ImageDto
                {
                    Id = Guid.NewGuid(),
                    Sizes = new Dictionary<string, ImageSizeDto>
                    {
                        { "Original", new ImageSizeDto { Url = "http://example.com/a", Width = -1, Height = -1 } },
                        { "Thumbnail", new ImageSizeDto { Url = "http://example.com/b", Width = 130, Height = 73 } },
                    },
                },
                Price = 79
            }
        };

        Console.WriteLine(JsonConvert.SerializeObject(items));
    }
}
[
    {
        "id": 6,
        "title": "Foo bar baz",
        "image": {
            "id": "6a1c1d34-3e78-4303-8edd-d8541dd915e7",
            "sizes": {
                "Original": {
                    "url": "http://example.com/a",
                    "w": -1,
                    "h": -1
                },
                "Thumbnail": {
                    "url": "http://example.com/b",
                    "w": 130,
                    "h": 73
                }
            }
        },
        "price": 79
    }
]