<legend id='N3qyv'><style id='N3qyv'><dir id='N3qyv'><q id='N3qyv'></q></dir></style></legend>
      1. <small id='N3qyv'></small><noframes id='N3qyv'>

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

        <tfoot id='N3qyv'></tfoot>

        在 ASP.NET Core MVC 中自定义响应序列化

        时间:2023-06-08
        <tfoot id='phpVh'></tfoot>
          <tbody id='phpVh'></tbody>

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

          <i id='phpVh'><tr id='phpVh'><dt id='phpVh'><q id='phpVh'><span id='phpVh'><b id='phpVh'><form id='phpVh'><ins id='phpVh'></ins><ul id='phpVh'></ul><sub id='phpVh'></sub></form><legend id='phpVh'></legend><bdo id='phpVh'><pre id='phpVh'><center id='phpVh'></center></pre></bdo></b><th id='phpVh'></th></span></q></dt></tr></i><div id='phpVh'><tfoot id='phpVh'></tfoot><dl id='phpVh'><fieldset id='phpVh'></fieldset></dl></div>
              • <bdo id='phpVh'></bdo><ul id='phpVh'></ul>
                  <legend id='phpVh'><style id='phpVh'><dir id='phpVh'><q id='phpVh'></q></dir></style></legend>

                  本文介绍了在 ASP.NET Core MVC 中自定义响应序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以自定义在 ASP.NET Core MVC 中将类型序列化为响应的方式?

                  Is it possible to customize the way types are serialized to the response in ASP.NET Core MVC?

                  在我的特定用例中,我有一个结构 AccountId,它简单地包裹着一个 Guid:

                  In my particular use case I've got a struct, AccountId, that simply wraps around a Guid:

                  public readonly struct AccountId
                  {
                      public Guid Value { get; }
                  
                      // ... 
                  }
                  

                  当我从动作方法返回它时,不出所料,它会序列化为以下内容:

                  When I return it from an action method, unsurprisingly, it serializes to the following:

                  { "value": "F6556C1D-1E8A-4D25-AB06-E8E244067D04" }
                  

                  相反,我想自动解开 Value 以便它序列化为纯字符串:

                  Instead, I'd like to automatically unwrap the Value so it serializes to a plain string:

                  "F6556C1D-1E8A-4D25-AB06-E8E244067D04"
                  

                  可以配置 MVC 来实现这一点吗?

                  Can MVC be configured to achieve this?

                  推荐答案

                  您可以使用 自定义转换器.

                  在你的情况下,它看起来像这样:

                  In your case, it would look like this:

                  [JsonConverter(typeof(AccountIdConverter))]
                  public readonly struct AccountId
                  {
                      public Guid Value { get; }
                  
                      // ... 
                  }
                  
                  public class AccountIdConverter : JsonConverter
                  {
                      public override bool CanConvert(Type objectType)
                          => objectType == typeof(AccountId);
                  
                      // this converter is only used for serialization, not to deserialize
                      public override bool CanRead => false;
                  
                      // implement this if you need to read the string representation to create an AccountId
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                          => throw new NotImplementedException();
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          if (!(value is AccountId accountId))
                              throw new JsonSerializationException("Expected AccountId object value.");
                  
                          // custom response 
                          writer.WriteValue(accountId.Value);
                      }
                  }
                  

                  如果您不想使用 JsonConverter 属性,可以在 ConfigureServices 中添加转换器(需要 Microsoft.AspNetCore.Mvc.Formatters.Json):

                  If you prefer not to use the JsonConverter attribute, it's possible to add converters in ConfigureServices (requires Microsoft.AspNetCore.Mvc.Formatters.Json):

                  public void ConfigureServices(IServiceCollection services)
                  {
                      services
                          .AddMvc()
                          .AddJsonOptions(options => {
                              options.SerializerSettings.Converters.Add(new AccountIdConverter());
                          });
                  }
                  

                  这篇关于在 ASP.NET Core MVC 中自定义响应序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                      <bdo id='XYklU'></bdo><ul id='XYklU'></ul>
                      <tfoot id='XYklU'></tfoot>

                        • <small id='XYklU'></small><noframes id='XYklU'>

                            <tbody id='XYklU'></tbody>
                          <i id='XYklU'><tr id='XYklU'><dt id='XYklU'><q id='XYklU'><span id='XYklU'><b id='XYklU'><form id='XYklU'><ins id='XYklU'></ins><ul id='XYklU'></ul><sub id='XYklU'></sub></form><legend id='XYklU'></legend><bdo id='XYklU'><pre id='XYklU'><center id='XYklU'></center></pre></bdo></b><th id='XYklU'></th></span></q></dt></tr></i><div id='XYklU'><tfoot id='XYklU'></tfoot><dl id='XYklU'><fieldset id='XYklU'></fieldset></dl></div>
                          <legend id='XYklU'><style id='XYklU'><dir id='XYklU'><q id='XYklU'></q></dir></style></legend>