Skip to content

Convert to netstandard2.0 sample #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>SharpGrip.FluentValidation.AutoValidation.Mvc</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<AssemblyName>SharpGrip.FluentValidation.AutoValidation.Mvc</AssemblyName>
<PackageId>SharpGrip.FluentValidation.AutoValidation.Mvc</PackageId>
<Title>SharpGrip FluentValidation AutoValidation MVC</Title>
Expand All @@ -25,7 +25,11 @@
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public async Task OnActionExecutionAsync(ActionExecutingContext actionExecutingC
{
if (IsValidController(actionExecutingContext.Controller))
{
var endpoint = actionExecutingContext.HttpContext.GetEndpoint();
var controllerActionDescriptor = (ControllerActionDescriptor) actionExecutingContext.ActionDescriptor;
var serviceProvider = actionExecutingContext.HttpContext.RequestServices;

#if NET
var endpoint = actionExecutingContext.HttpContext.GetEndpoint();
if (endpoint != null &&
((autoValidationMvcConfiguration.ValidationStrategy == ValidationStrategy.Annotations &&
!endpoint.Metadata.OfType<FluentValidationAutoValidationAttribute>().Any() && !endpoint.Metadata.OfType<AutoValidationAttribute>().Any()) ||
Expand All @@ -49,7 +49,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext actionExecutingC

return;
}

#endif
foreach (var parameter in controllerActionDescriptor.Parameters)
{
if (actionExecutingContext.ActionArguments.TryGetValue(parameter.Name, out var subject))
Expand Down Expand Up @@ -108,9 +108,12 @@ public async Task OnActionExecutionAsync(ActionExecutingContext actionExecutingC

if (!actionExecutingContext.ModelState.IsValid)
{
#if NET
var problemDetailsFactory = serviceProvider.GetRequiredService<ProblemDetailsFactory>();
var validationProblemDetails = problemDetailsFactory.CreateValidationProblemDetails(actionExecutingContext.HttpContext, actionExecutingContext.ModelState);

#else
var validationProblemDetails = new ValidationProblemDetails(actionExecutingContext.ModelState);
#endif
actionExecutingContext.Result = fluentValidationAutoValidationResultFactory.CreateActionResult(actionExecutingContext, validationProblemDetails);

return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
#if !NET
using Microsoft.AspNetCore.Mvc.Internal;
#endif
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.AspNetCore.Mvc;
#if !NET
using Microsoft.AspNetCore.Mvc.Internal;
#endif
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;

Expand All @@ -25,7 +28,7 @@ public override bool Validate(ModelMetadata? metadata, string? key, object? mode
return disableBuiltInModelValidation || base.Validate(metadata, key, model, alwaysValidateAtTopLevel);
}

#if !NETCOREAPP3_1
#if NET
public override bool Validate(ModelMetadata? metadata, string? key, object? model, bool alwaysValidateAtTopLevel, object? container)
{
// If built in model validation is disabled return true for later validation in the action filter.
Expand Down