日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

【转】C# WebAPI中为自定义模型设置JSonConverter

發(fā)布時間:2023/12/10 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】C# WebAPI中为自定义模型设置JSonConverter 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我的WebAPI應(yīng)用程序中有一個模型,用.NET 4.0編寫,含有一個System.Net.Mime.ContentType類型的屬性,如下所示:

?

[Serializable] public class FileData {private ContentType contentType;private long size;private string name;public ContentType ContentType{get { return contentType; }set { contentType = value; } }.../* same getter/setter logic for the other fields */ }

該模型的定義位于我的Web項目的另一個程序集中.

所以,客戶端發(fā)給我一個JSON消息,我需要轉(zhuǎn)換為這個類:

?

{"contentType": "image/png","size": 12345,"name": "avatar.png" }

為了告訴Json.NET如何轉(zhuǎn)換ContentType,我注冊了一個自定義的JsonConverter:

?

JsonFormatter.SerializerSettings.Converters.Add(new ContentTypeJsonConverter());

在上面的代碼中,我指的是WebApi應(yīng)用程序的全局JsonFormatter對象.

因此,當客戶端向我發(fā)送JSON時,我希望控制器能夠正確地解析消息.

不幸的是,它失敗并出現(xiàn)錯誤:

?

“Could not cast or convert from System.String to System.Net.Mime.ContentType.”

我知道我可以通過將以下代碼添加到我的FileData類來解決這個問題:

?

public class FileData {...[JsonConverter(typeof(ContentTypeJsonConverter))]public ContentType ContentType { /* Getter and Setter */ } }

但問題是我不能在FileData類型所在的程序集中向JSON.NET引入依賴項.

有沒有辦法在不改變FileData類的情況下觸發(fā)contentType成員的正確反序列化?

除了以上我還嘗試了Brian Rogers建議:

?

JsonFormatter.SerializerSettings.ContractResolver = new CustomResolver();

使用以下CustomResolver實現(xiàn):

?

class CustomResolver : DefaultContractResolver {protected override JsonContract CreateContract(Type objectType){var contract = base.CreateContract(objectType);if (objectType == typeof(ContentType)){contract.Converter = new ContentTypeJsonConverter();}return contract;} }

結(jié)果仍然相同.

最佳答案 以下適用于我(Web API 2).

模型:

?

[Serializable] public class FileData {private ContentType contentType;public ContentType ContentType{get { return contentType; }set { contentType = value; }} }

自定義JSON轉(zhuǎn)換器:

?

public class ContentTypeJsonConverter : JsonConverter {public override bool CanConvert(Type objectType){return objectType == typeof(ContentType);}public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer){return new ContentType((string)reader.Value);}public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer){writer.WriteValue(((ContentType)value).ToString());} }

轉(zhuǎn)換器注冊(WebApiConfig.cs):

?

public static void Register(HttpConfiguration config) {...config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new ContentTypeJsonConverter()); }

控制器:

?

public class TestController : ApiController {public IHttpActionResult Post(FileData data){return this.Ok(data);} }

請求:

?

POST /api/test HTTP/1.1 Content-Type: application/json Host: localhost:48278 Content-Length: 36{"contentType": "image/png" }

響應(yīng):

?

HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?ZDpcd29ya1xUb0REXGFwaVx0ZXN0?= X-Powered-By: ASP.NET Date: Mon, 25 Jul 2016 07:06:02 GMT Content-Length: 27{"contentType":"image/png"}

????本文轉(zhuǎn)自網(wǎng)絡(luò)文章,轉(zhuǎn)載此文章僅為分享知識,如有侵權(quán),請聯(lián)系博主進行刪除。

總結(jié)

以上是生活随笔為你收集整理的【转】C# WebAPI中为自定义模型设置JSonConverter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。