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

    1. <tfoot id='3UKYM'></tfoot>

      1. Aspnet Core 十进制绑定不适用于非英语文化

        时间:2023-08-25
            <tbody id='YTwxC'></tbody>
          <tfoot id='YTwxC'></tfoot>
          1. <i id='YTwxC'><tr id='YTwxC'><dt id='YTwxC'><q id='YTwxC'><span id='YTwxC'><b id='YTwxC'><form id='YTwxC'><ins id='YTwxC'></ins><ul id='YTwxC'></ul><sub id='YTwxC'></sub></form><legend id='YTwxC'></legend><bdo id='YTwxC'><pre id='YTwxC'><center id='YTwxC'></center></pre></bdo></b><th id='YTwxC'></th></span></q></dt></tr></i><div id='YTwxC'><tfoot id='YTwxC'></tfoot><dl id='YTwxC'><fieldset id='YTwxC'></fieldset></dl></div>

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

          2. <legend id='YTwxC'><style id='YTwxC'><dir id='YTwxC'><q id='YTwxC'></q></dir></style></legend>
                <bdo id='YTwxC'></bdo><ul id='YTwxC'></ul>

                  本文介绍了Aspnet Core 十进制绑定不适用于非英语文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个使用非英语配置(西班牙语)运行的 aspnet 核心应用程序:

                  I have an aspnet core app that runs with a non english configuration (spanish):

                  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
                      {
                          ......
                          app.UseRequestLocalization(new RequestLocalizationOptions
                          {
                              DefaultRequestCulture = new RequestCulture(new CultureInfo("es-AR"))
                              ,SupportedCultures = new List<CultureInfo>
                              {
                                  new CultureInfo("es-AR")
                              }
                              ,SupportedUICultures = new List<CultureInfo>
                              {
                                  new CultureInfo("es")
                              }
                          });
                  
                          .........
                      }
                  

                  在英语中,十进制数的小数部分用点分隔,但在西班牙语中使用逗号:

                  In english a decimal number has its decimal part delimited with a dot, but in spanish a comma is used:

                  • 10256.35 英文
                  • 10256,35 西班牙语

                  我在控制器中有这个动作:

                  I have this action in a controller:

                   [HttpPost]
                   public decimal Test(decimal val)
                   {
                       return val;
                   }
                  

                  如果我使用邮递员并向该操作发送一个像 {val: 15.30} 这样的 json,那么操作中的 val 会收到 0(由于文化,绑定不起作用).如果我发送这样的 json {val: 15,30} 然后在操作中我收到 15.30我遇到的问题是,我需要接受带逗号的小数的操作,因为这是来自应用程序表单中输入类型文本的格式.但我还需要接受带有来自 json 格式请求的点的小数.无法在接受逗号的 json 中指定小数/浮点数(不能将其作为字符串发送).我怎样才能做到这一点???我快把自己逼疯了.

                  If I use postman and send to that action a json like this {val: 15.30}, then val in the action recives a 0 (binding not working because of the culture). If I send a json like this {val: 15,30} then in the action I recive 15.30 The problem I have is, I need the action to accept decimals with commas, because that is the format that comes from inputs type text in the app's forms. But i also need to accept decimal with a dot that comes from request in json format. There is no way to specify a decimal/float in json that accepts a comma (send it as string is not an option). How can I do this??? I'm driving my self crazy with this.

                  谢谢!!

                  推荐答案

                  显然,ASP.NET core 1.0.0 中的十进制绑定默认不是文化不变的.模型绑定取决于服务器文化.

                  Apparently, the decimal binding in ASP.NET core 1.0.0 is not culture invariant by default. The model binding depends on the server culture.

                  您可以按照 Stephen Muecke 的建议使用自定义模型绑定来更改此行为.这是我的基于 自定义模型绑定在 ASP.Net核心 1.0 (RTM)

                  You can change this behavior with a custom model binding as suggested by Stephen Muecke. Here is mine based on Custom Model Binding in ASP.Net Core 1.0 (RTM)

                  public class InvariantDecimalModelBinderProvider : IModelBinderProvider
                  {
                      public IModelBinder GetBinder(ModelBinderProviderContext context)
                      {
                          if (context == null) throw new ArgumentNullException(nameof(context));
                  
                          if (!context.Metadata.IsComplexType && (context.Metadata.ModelType == typeof(decimal) || context.Metadata.ModelType == typeof(decimal?)))
                          {
                              return new InvariantDecimalModelBinder(context.Metadata.ModelType);
                          }
                  
                          return null;
                      }
                  }
                  
                  public class InvariantDecimalModelBinder : IModelBinder
                  {
                      private readonly SimpleTypeModelBinder _baseBinder;
                  
                      public InvariantDecimalModelBinder(Type modelType)
                      {
                          _baseBinder = new SimpleTypeModelBinder(modelType);
                      }
                  
                      public Task BindModelAsync(ModelBindingContext bindingContext)
                      {
                          if (bindingContext == null) throw new ArgumentNullException(nameof(bindingContext));
                  
                          var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                  
                          if (valueProviderResult != ValueProviderResult.None)
                          {
                              bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
                  
                              var valueAsString = valueProviderResult.FirstValue;
                              decimal result;
                  
                              // Use invariant culture
                              if (decimal.TryParse(valueAsString, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result))
                              {
                                  bindingContext.Result = ModelBindingResult.Success(result);
                                  return Task.CompletedTask;
                              }
                          }
                  
                          // If we haven't handled it, then we'll let the base SimpleTypeModelBinder handle it
                          return _baseBinder.BindModelAsync(bindingContext);
                      }
                  }
                  

                  在 Startup.cs 中:

                  And in Startup.cs:

                  services.AddMvc(config =>
                  {
                      config.ModelBinderProviders.Insert(0, new InvariantDecimalModelBinderProvider());
                  });
                  

                  这篇关于Aspnet Core 十进制绑定不适用于非英语文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:搜索字符串文字 下一篇:如何在 WPF 应用程序中全局设置文化?

                  相关文章

                  最新文章

                  1. <tfoot id='0yFBH'></tfoot>
                    <legend id='0yFBH'><style id='0yFBH'><dir id='0yFBH'><q id='0yFBH'></q></dir></style></legend>
                  2. <i id='0yFBH'><tr id='0yFBH'><dt id='0yFBH'><q id='0yFBH'><span id='0yFBH'><b id='0yFBH'><form id='0yFBH'><ins id='0yFBH'></ins><ul id='0yFBH'></ul><sub id='0yFBH'></sub></form><legend id='0yFBH'></legend><bdo id='0yFBH'><pre id='0yFBH'><center id='0yFBH'></center></pre></bdo></b><th id='0yFBH'></th></span></q></dt></tr></i><div id='0yFBH'><tfoot id='0yFBH'></tfoot><dl id='0yFBH'><fieldset id='0yFBH'></fieldset></dl></div>

                    <small id='0yFBH'></small><noframes id='0yFBH'>

                      • <bdo id='0yFBH'></bdo><ul id='0yFBH'></ul>