久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<tfoot id='lvjDw'></tfoot>

<small id='lvjDw'></small><noframes id='lvjDw'>

  • <i id='lvjDw'><tr id='lvjDw'><dt id='lvjDw'><q id='lvjDw'><span id='lvjDw'><b id='lvjDw'><form id='lvjDw'><ins id='lvjDw'></ins><ul id='lvjDw'></ul><sub id='lvjDw'></sub></form><legend id='lvjDw'></legend><bdo id='lvjDw'><pre id='lvjDw'><center id='lvjDw'></center></pre></bdo></b><th id='lvjDw'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='lvjDw'><tfoot id='lvjDw'></tfoot><dl id='lvjDw'><fieldset id='lvjDw'></fieldset></dl></div>
      • <bdo id='lvjDw'></bdo><ul id='lvjDw'></ul>

      <legend id='lvjDw'><style id='lvjDw'><dir id='lvjDw'><q id='lvjDw'></q></dir></style></legend>

      1. 每個請求的 ASP.NET Core API JSON 序列化程序設置

        ASP.NET Core API JSON serializersettings per request(每個請求的 ASP.NET Core API JSON 序列化程序設置)
          <legend id='UOzmX'><style id='UOzmX'><dir id='UOzmX'><q id='UOzmX'></q></dir></style></legend>

          <i id='UOzmX'><tr id='UOzmX'><dt id='UOzmX'><q id='UOzmX'><span id='UOzmX'><b id='UOzmX'><form id='UOzmX'><ins id='UOzmX'></ins><ul id='UOzmX'></ul><sub id='UOzmX'></sub></form><legend id='UOzmX'></legend><bdo id='UOzmX'><pre id='UOzmX'><center id='UOzmX'></center></pre></bdo></b><th id='UOzmX'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='UOzmX'><tfoot id='UOzmX'></tfoot><dl id='UOzmX'><fieldset id='UOzmX'></fieldset></dl></div>
            • <bdo id='UOzmX'></bdo><ul id='UOzmX'></ul>

                  <small id='UOzmX'></small><noframes id='UOzmX'>

                    <tbody id='UOzmX'></tbody>
                  <tfoot id='UOzmX'></tfoot>
                  本文介紹了每個請求的 ASP.NET Core API JSON 序列化程序設置的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  基于請求中的某些值(標頭或 url),我想更改我的 DTO 對象的序列化.為什么?好吧,我已將 [JsonProperty("A")] 應用于我的 DTO,但取決于它是否要使用該屬性的客戶端(網站或移動應用程序).我開始了

                  Based on some value in the request (header or in the url) I want to change the serialization of my DTO objects. Why? Well I've applied the [JsonProperty("A")] to my DTO's but depending on the client (website or mobile app) it want to use that property or not. I started with

                  services
                  .AddMvc()
                  .AddJsonOptions(opt =>
                  {
                  #if DEBUG
                      opt.SerializerSettings.ContractResolver = new NoJsonPropertyNameContractResolver();
                  #endif
                  }
                  

                  所以在調試時,我得到了帶有完整屬性名的 JSON.我使用 JsonProperty 屬性來縮短響應 JSON,它適用于反序列化回相同 DTO 的移動應用程序 (Xamarin).但是現在我有一個網站,它使用相同的 API 通過 jQuery 獲取數據,但在那里我想處理 DTO 的完整屬性名稱,而不是 JsonProperty 屬性中給出的名稱.網站和WebApi在同一個服務器,響應大一點也沒問題.

                  So while debugging I get JSON with full propertynames. I use the JsonProperty attribute to shorten the response JSON, which works fine with the mobile app (Xamarin) which deserialize back to the same DTO's. But now I have a website which uses the the same API to get data via jQuery, but in there I want to deal with the full property names of the DTO's, not the name given in the JsonProperty attribute. Website and WebApi are on the same server so it's no problem if the response is a little bigger.

                  我從一個中間件類開始對客戶標頭值做出反應,這很有效,但現在我不知道如何訪問 JSON SerializerSettings.在網上搜索但找不到.

                  I started with a middleware class to react on a customer header value, which works, but now I don't know how to get to the JSON SerializerSettings. Searched the web but cannot find it.

                  在搜索時,我閱讀了有關 InputFormatters 和 OutputFormatters 以及內容協商的信息,但我不知道該往哪個方向發展.

                  While searching I've read about InputFormatters and OutputFormatters, and also content negotiation, but I don't know which direction I must go.

                  我不想使用不同的設置兩次部署相同的 API.
                  如果有幫助,我可以更改諸如 routesconfig 之類的內容.

                  I don't want to deploy the same API twice with different settings.
                  I'am able to change things like the routesconfig if that would help.

                  更新
                  不僅 JSON 響應必須以兩種不同的方式進行序列化,反序列化也必須以兩種不同的方式進行.

                  Update
                  Not only the JSON response had to be serialized in 2 different ways, also the deserializing had to be done in 2 different ways.

                  推薦答案

                  感謝您的意見和回答.我找到了輸入和輸出格式化程序的解決方案.感謝 http://rovani.net/Explicit-Model-Constructor/ 指出我在正確的方向.

                  Thanks for the comments and answers. I found a solution with Input and outputformatters. With thanks to http://rovani.net/Explicit-Model-Constructor/ to point me in the right direction.

                  我創建了自己的輸入和輸出格式化程序,它們繼承自 JsonInputFormatter 以保持盡可能多的功能相同.
                  在構造函數中,我設置了支持的媒體類型(使用了一些看起來像現有 JSON 的媒體類型).
                  還必須重寫 CreateJsonSerializer 以將 ContractResolver 設置為所需的(可以實現單例).
                  必須這樣做,因為更改構造函數中的 serializerSettings 會更改所有輸入/輸出格式化程序的序列化程序設置,這意味著默認的 JSON 格式化程序也將使用新的合約解析器.
                  這樣做也意味著您可以通過 AddMvc().AddJsonOption()

                  I've created my own input and outputformatters, which inherit from JsonInputFormatter to keep as much functionality the same.
                  In the constructor I set the supported mediatype (used some that looks like the existing one for JSON).
                  Also must override CreateJsonSerializer to set the ContractResolver to the desired one (could implement singleton).
                  Must do it this way, because changing the serializerSettings in the constructor would change the serializersettings for all input/outputformatters, meaning the default JSON formatters will also use the new contract resolver.
                  Also doing it this way means you can setup some default JSON options via AddMvc().AddJsonOption()

                  示例inputformatter,outputformatter使用相同的原理:

                  Example inputformatter, outputformatter uses the same principle:

                  static MediaTypeHeaderValue protoMediaType = MediaTypeHeaderValue.Parse("application/jsonfull");
                  
                  public JsonFullInputFormatter(ILogger logger, JsonSerializerSettings serializerSettings, ArrayPool<char> charPool, ObjectPoolProvider objectPoolProvider) 
                      : base(logger, serializerSettings, charPool, objectPoolProvider)
                  {
                      this.SupportedMediaTypes.Clear();
                      this.SupportedMediaTypes.Add(protoMediaType);
                  }
                  
                  protected override JsonSerializer CreateJsonSerializer()
                  {
                      var serializer = base.CreateJsonSerializer();            
                      serializer.ContractResolver = new NoJsonPropertyNameContractResolver();
                  
                      return serializer;
                  }
                  

                  根據上面提到的安裝類 URL:

                  As per the mentioned URL above the setup class:

                  public class YourMvcOptionsSetup : IConfigureOptions<MvcOptions>
                  {
                      private readonly ILoggerFactory _loggerFactory;
                      private readonly JsonSerializerSettings _jsonSerializerSettings;
                      private readonly ArrayPool<char> _charPool;
                      private readonly ObjectPoolProvider _objectPoolProvider;
                  
                      public YourMvcOptionsSetup(ILoggerFactory loggerFactory, IOptions<MvcJsonOptions> jsonOptions, ArrayPool<char> charPool, ObjectPoolProvider objectPoolProvider)
                      {
                          //Validate parameters and set fields
                      }
                  
                      public void Configure(MvcOptions options)
                      {
                          var jsonFullInputFormatter = new JsonFullInputFormatter(
                              _loggerFactory.CreateLogger<JsonFullInputFormatter>(),
                              _jsonSerializerSettings,
                              _charPool,
                              _objectPoolProvider
                          );
                  
                          options.InputFormatters.Add(jsonFullInputFormatter);
                  
                          options.OutputFormatters.Add(new JsonFullOutputFormatter(
                              _jsonSerializerSettings,
                              _charPool
                          ));
                      }
                  

                  然后是一個擴展方法來注冊它:

                  And then an extension method to register it:

                  public static class MvcBuilderExtensions
                  {
                      public static IMvcBuilder AddJsonFullFormatters(this IMvcBuilder builder)
                      {
                          if (builder == null)
                          {
                              throw new ArgumentNullException(nameof(builder));
                          }
                          ServiceDescriptor descriptor = ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, YourMvcOptionsSetup>();
                          builder.Services.TryAddEnumerable(descriptor);
                          return builder;
                      }
                  }
                  

                  ConfigureServices中調用它:

                  services.AddMvc(config =>
                  {
                      config.RespectBrowserAcceptHeader = true; // To use the JsonFullFormatters if clients asks about it via Accept Header
                  })
                  .AddJsonFullFormatters() //Add our own JSON Formatters
                  .AddJsonOptions(opt =>
                  {
                       //Set up some default options all JSON formatters must use (if any)
                  });
                  

                  現在我們的 Xamarin 應用可以訪問 webapi 并接收帶有通過 JsonProperty 屬性設置的(短)屬性名稱的 JSON.
                  在網站中,我們可以通過添加 Accept(get 調用)和 ContentType(post/put 調用)標頭來獲取完整的 JSON 屬性名稱.我們通過 jQuery 的 $.ajaxSetup(.

                  Now our Xamarin App can access the webapi and receive JSON with (short) property names set via JsonProperty attribute.
                  And in the website we can get the full JSON property names by adding an Accept (get calls) and ContentType (post/put calls) header. Which we do once via jQuery's $.ajaxSetup(.

                  $.ajaxSetup({
                      contentType: "application/jsonfull; charset=utf-8",
                      headers: { 'Accept': 'application/jsonfull' }
                  });
                  

                  這篇關于每個請求的 ASP.NET Core API JSON 序列化程序設置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)
                1. <i id='Dd3c9'><tr id='Dd3c9'><dt id='Dd3c9'><q id='Dd3c9'><span id='Dd3c9'><b id='Dd3c9'><form id='Dd3c9'><ins id='Dd3c9'></ins><ul id='Dd3c9'></ul><sub id='Dd3c9'></sub></form><legend id='Dd3c9'></legend><bdo id='Dd3c9'><pre id='Dd3c9'><center id='Dd3c9'></center></pre></bdo></b><th id='Dd3c9'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Dd3c9'><tfoot id='Dd3c9'></tfoot><dl id='Dd3c9'><fieldset id='Dd3c9'></fieldset></dl></div>
                    <tbody id='Dd3c9'></tbody>
                  • <bdo id='Dd3c9'></bdo><ul id='Dd3c9'></ul>

                        • <legend id='Dd3c9'><style id='Dd3c9'><dir id='Dd3c9'><q id='Dd3c9'></q></dir></style></legend>

                          <tfoot id='Dd3c9'></tfoot>
                          • <small id='Dd3c9'></small><noframes id='Dd3c9'>

                          • 主站蜘蛛池模板: 日本a v在线播放 | 久久国产精 | 欧美成人一级 | 中文字幕精品一区久久久久 | 中文在线观看视频 | 成人福利在线观看 | 四虎色播 | 黄色1级视频 | 日韩理论视频 | 97超碰免费| 亚洲天堂免费视频 | 国产日产精品一区二区三区的介绍 | 亚洲日本精品 | 色综合av| 涩涩999| 国产理论片| 成人国产精品视频 | 日韩精品久久久久 | 欧美一级淫片免费视频黄 | 精品视频免费观看 | 在线成人免费视频 | 黄色片视频网站 | 国产精品久久久一区二区三区 | 国产一区二三区 | 欧美a级黄色片 | 久久久久久久久国产 | 欧美日韩视频 | 9l视频自拍九色9l视频成人 | 欧美成人高清 | 国模一区二区 | 国产精品毛片av | 日本激情网站 | 中文字幕在线观看免费视频 | 亚洲成人a v | 国产黄色在线 | 日本a v在线播放 | 成人免费视频一区二区 | 天天操夜夜骑 | 手机av在线 | 日韩视频免费看 | 国产成人久久精品麻豆二区 |