Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

The FluentValidation.AspNetCore package is no longer being maintained and is now unsupported - move to the core FluentValidation package

support2a14047e314d
diff --git a/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj b/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
index c2438fec1..cb904fb13 100644
--- a/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
+++ b/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\Build\Grand.Common.props" />
<ItemGroup>
+ <PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.7.1" />
<PackageReference Include="GoogleAuthenticator" Version="3.1.0" />
<PackageReference Include="MassTransit" Version="8.0.16" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.0.16" />
@@ -18,7 +19,7 @@
<PackageReference Include="Scrutor" Version="4.2.2" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
- <PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
+ <PackageReference Include="FluentValidation" Version="11.7.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Grand.Domain\Grand.Domain.csproj" />
diff --git a/src/Core/Grand.Infrastructure/StartupBase.cs b/src/Core/Grand.Infrastructure/StartupBase.cs
index 81d7a797a..415d46740 100644
--- a/src/Core/Grand.Infrastructure/StartupBase.cs
+++ b/src/Core/Grand.Infrastructure/StartupBase.cs
@@ -1,4 +1,5 @@
using AutoMapper;
+using FluentValidation;
using Grand.Domain.Data;
using Grand.Infrastructure.Caching.RabbitMq;
using Grand.Infrastructure.Configuration;
@@ -12,7 +13,6 @@ using Grand.Infrastructure.Validators;
using Grand.SharedKernel;
using Grand.SharedKernel.Extensions;
using MassTransit;
-using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
@@ -197,7 +197,8 @@ namespace Grand.Infrastructure
/// </summary>
/// <param name="services">Collection of service descriptors</param>
/// <param name="configuration">Configuration</param>
- private static IMvcCoreBuilder RegisterApplication(IServiceCollection services, IConfiguration configuration)
+ /// <param name="typeSearcher">Type searcher</param>
+ private static IMvcCoreBuilder RegisterApplication(IServiceCollection services, IConfiguration configuration, ITypeSearcher typeSearcher)
{
//add accessor to HttpContext
services.AddHttpContextAccessor();
@@ -233,7 +234,7 @@ namespace Grand.Infrastructure
CommonHelper.IgnoreAcl = performanceConfig.IgnoreAcl;
CommonHelper.IgnoreStoreLimitations = performanceConfig.IgnoreStoreLimitations;
-
+
services.AddTransient<FluentValidationFilter>();
var mvcCoreBuilder = services.AddMvcCore(options =>
{
@@ -261,16 +262,16 @@ namespace Grand.Infrastructure
/// <param name="configuration">Configuration root of the application</param>
public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
+ //find startup configurations provided by other assemblies
+ var typeSearcher = new TypeSearcher();
+ services.AddSingleton<ITypeSearcher>(typeSearcher);
+
//register application
- var mvcBuilder = RegisterApplication(services, configuration);
+ var mvcBuilder = RegisterApplication(services, configuration, typeSearcher);
//register extensions
RegisterExtensions(mvcBuilder, configuration);
- //find startup configurations provided by other assemblies
- var typeSearcher = new TypeSearcher();
- services.AddSingleton<ITypeSearcher>(typeSearcher);
-
var startupConfigurations = typeSearcher.ClassesOfType<IStartupApplication>();
//Register startup
diff --git a/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs b/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs
index 8ee83a3fa..61b7d7cdc 100644
--- a/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs
+++ b/src/Core/Grand.Infrastructure/Validators/FluentValidationFilter.cs
@@ -1,5 +1,4 @@
using FluentValidation;
-using FluentValidation.AspNetCore;
using Grand.SharedKernel.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@@ -49,7 +48,11 @@ namespace Grand.Infrastructure.Validators
var result = await validator.ValidateAsync(contextValidator);
if (result.IsValid) continue;
- result.AddToModelState(context.ModelState, "");
+ if (!result.IsValid) {
+ foreach (var error in result.Errors) {
+ context.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
+ }
+ }
var hasJsonData = context.HttpContext.Request.ContentType?.Contains("application/json") ?? false;
if (!hasJsonData) continue;
context.Result = new BadRequestObjectResult(context.ModelState);
diff --git a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
index 2366b804a..9f851df28 100644
--- a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
+++ b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
@@ -1,6 +1,5 @@
using Azure.Identity;
using FluentValidation;
-using FluentValidation.AspNetCore;
using Grand.Business.Core.Interfaces.Authentication;
using Grand.Business.Core.Interfaces.Common.Configuration;
using Grand.Business.Core.Interfaces.Common.Security;
@@ -254,7 +253,6 @@ namespace Grand.Web.Common.Infrastructure
}
//Add fluentValidation
- services.AddFluentValidationClientsideAdapters();
var typeSearcher = new TypeSearcher();
var assemblies = typeSearcher.GetAssemblies();
services.AddValidatorsFromAssemblies(assemblies);
@@ -265,7 +263,7 @@ namespace Grand.Web.Common.Infrastructure
//register controllers as services, it'll allow to override them
mvcBuilder.AddControllersAsServices();
-
+
return mvcBuilder;
}