简体   繁体   English

WebAPI 2 ODATA 3 C#项目中URI类型的JSON序列化

[英]JSON Serialization of URI Type in WebAPI 2 ODATA 3 C# Project

I'm seeing C# URI types serialized to JSON in an ODATA 3 controller in my WebAPI 2 project as an array of segments that does not include the domain. 我在WebAPI 2项目的ODATA 3控制器中看到序列化为JSON的C#URI类型为不包含域的段数组。 I've tried everything I can think of to change this (including fiddling with the serialization settings and even trying out the contract serializer instead of JSON.Net). 我已经尝试了所有可以想到的更改它的方法(包括摆弄序列化设置,甚至尝试使用合约序列化器而不是JSON.Net)。 Nothing seems to change the behavior. 似乎没有什么改变行为。 Note, I am not using .Net Core. 注意,我没有使用.Net Core。 Here is a code sample, condensed into a single snippet. 这是一个代码示例,压缩为一个片段。

namespace WebApplication1.Controllers
{
    public class MyObject
    {
        public Uri Url { get; set; }

        public string Name { get; set; }

        public string ID { get; set; }
    }

    public class MyObjectsController : ODataController
    {
        private static ODataValidationSettings _validationSettings = new ODataValidationSettings();

        public IHttpActionResult GetMyObjects(ODataQueryOptions<MyObject> queryOptions)
        {
            try
            {
                queryOptions.Validate(_validationSettings);

                return Ok<IEnumerable<MyObject>>(new List<MyObject>() { new MyObject() { ID="asdf", Name="123rwe", Url = new Uri("http://www.webapp.com/sites/page.html") } });
            }
            catch (ODataException ex)
            {
                return BadRequest(ex.Message);
            }
        }

    }
}

This generates the following JSON in the browser: 这将在浏览器中生成以下JSON:

{
  "odata.metadata":"http://localhost:51607/odata/$metadata#MyObjects","value":[
    {
      "Url":{
        "Segments":[
          "/","sites/","page.html"
        ]
      },"Name":"123rwe","ID":"asdf"
    }
  ]
}

This is what I'd like (without changing the Url property to a string): 这就是我想要的(不将Url属性更改为字符串):

{
  "odata.metadata":"http://localhost:51607/odata/$metadata#MyObjects","value":[
    {
      "Url":"http://www.webapp.com/sites/page.html","Name":"123rwe","ID":"asdf"
    }
  ]
}

Any thoughts? 有什么想法吗?

UPDATE: Further research is suggesting that serialization behavior for URI types in WebAPI ODATA is controlled by the odataentityreferencelink and odataentityreferencelinkserializer classes. 更新:进一步的研究表明,WebAPI ODATA中URI类型的序列化行为由odataentityreferencelink和odataentityreferencelinkserializer类控制。 Specifically, URI type appear to be converted to odataentityreferencelink types which are then serialized in the manner I posted above (as an array of segments not including the root domain). 具体来说,URI类型似乎转换为odataentityreferencelink类型,然后按照我上面发布的方式进行序列化(作为不包括根域的段数组)。 I still need to know how to change this behavior, however the documentation for these two classes is not proving helpful. 我仍然需要知道如何更改此行为,但是事实证明这两个类的文档没有帮助。 Last, I've confirmed this problem is not specific to the JSON output format. 最后,我已经确认此问题并非特定于JSON输出格式。 The serialization behavior for both XML/Atom and JSON are identical: URIs are broken down into an array of segments. XML / Atom和JSON的序列化行为是相同的:URI被细分为段数组。

MS Premier support provided a final answer to this which I'll share below. MS Premier支持对此提供了最终答案,我将在下面分享。

  1. There is no option to directly JSON serialize an URI type, normally it would be broken into array of segments as you are observing in your code 没有选择直接对URI类型进行JSON序列化,通常会在您观察代码时将其分成段数组
  2. The domain name will be eliminated as a normal scenario 正常情况下,域名将被删除
  3. The option you can go for is to create a custom URI Converter deriving from JSONConverter which is a part of Newtonsoft.Json namespace 您可以选择的选项是创建一个自JSONConverter派生的自定义URI转换器,该转换器是Newtonsoft.Json命名空间的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM