diff --git a/services/book-store/BookStoreModule.cs b/services/book-store/BookStoreModule.cs index 9f3cee4..f0b507f 100644 --- a/services/book-store/BookStoreModule.cs +++ b/services/book-store/BookStoreModule.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -12,9 +13,9 @@ using Sanhe.Abp.AspNetCore.Mvc.Wrapper; using Sanhe.Abp.ExceptionHandling; using Sanhe.Abp.ExceptionHandling.Emailing; using Sanhe.Abp.Features.LimitValidation.Redis; -using Sanhe.Abp.Wrapper; using StackExchange.Redis; using System; +using System.Collections.Generic; using System.Linq; using Volo.Abp; using Volo.Abp.Account; @@ -132,11 +133,11 @@ public class BookStoreModule : AbpModule context.Services.AddAlwaysAllowAuthorization(); // wrap - Configure(options => - { - options.IsEnabled = true; - options.IgnoreNamespaces.Clear(); - }); + //Configure(options => + //{ + // options.IsEnabled = true; + // options.IgnoreNamespaces.Clear(); + //}); // limit Configure(options => { @@ -231,14 +232,14 @@ public class BookStoreModule : AbpModule Configure(options => { options.Resources - .Add("en") + .Add("zh-Hans") .AddBaseTypes(typeof(AbpValidationResource)) .AddVirtualJson("/Localization/BookStore"); options.DefaultResourceType = typeof(BookStoreResource); - //options.Languages.Add(new LanguageInfo("en", "en", "English")); options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); + options.Languages.Add(new LanguageInfo("en", "en", "English")); }); Configure(options => @@ -262,10 +263,10 @@ public class BookStoreModule : AbpModule private void ConfigureAutoApiControllers() { - Configure(options => - { - //options.ConventionalControllers.Create(typeof(BookStoreModule).Assembly); - }); + //Configure(options => + //{ + // options.ConventionalControllers.Create(typeof(BookStoreModule).Assembly); + //}); } private void ConfigureSwagger(IServiceCollection services) @@ -355,7 +356,11 @@ public class BookStoreModule : AbpModule app.UseDeveloperExceptionPage(); } - app.UseAbpRequestLocalization(optios => optios.SetDefaultCulture("zh-hans")); + // 微软内置从三个地方获取本地化:QueryString、Cookie、Header:accept-heade + // 浏览器中文的Culture字符串是zh-CN与Abp的zh-Hans不匹配 + // 故而清除此provider + app.UseAbpRequestLocalization( + options => options.RequestCultureProviders.RemoveAll(provider => provider is AcceptLanguageHeaderRequestCultureProvider)); if (!env.IsDevelopment()) { diff --git a/services/book-store/Controllers/DataAnnotationsLocalizationController.cs b/services/book-store/Controllers/DataAnnotationsLocalizationController.cs new file mode 100644 index 0000000..c8709a2 --- /dev/null +++ b/services/book-store/Controllers/DataAnnotationsLocalizationController.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.AspNetCore.Mvc; + +namespace BookStore.Controllers; + +// 验证数据模型验证本地化 +// 配合AbpMvcDataAnnotationsLocalizationOptions及AbpLocalizationOptions.DefaultResourceType +[Route("api/data-annotation")] +[RemoteService] +public class DataAnnotationsLocalizationController: AbpControllerBase +{ + [HttpPost] + public Task CreateAsync(Person person) + { + return Task.FromResult(person); + } +} \ No newline at end of file diff --git a/services/book-store/Controllers/WrapController.cs b/services/book-store/Controllers/WrapController.cs index 8511b96..0caa706 100644 --- a/services/book-store/Controllers/WrapController.cs +++ b/services/book-store/Controllers/WrapController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; @@ -42,6 +43,9 @@ public class WrapController : AbpControllerBase public class Person { + [Required] + [StringLength(100, MinimumLength = 1)] public string Name { get; set; } + [Range(0, 150)] public int Age { get; set; } } \ No newline at end of file