Skip to content

Commit

Permalink
fix ValidDtoFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Jul 23, 2024
1 parent fed5d10 commit 8dda8e2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
20 changes: 17 additions & 3 deletions Biwen.Settings.TestWebUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using Biwen.Settings.Encryption;
using Biwen.Settings.TestWebUI.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.HttpLogging;

var builder = WebApplication.CreateBuilder(args);


// Add services to the container.
builder.Services.AddRazorPages();

Expand All @@ -23,6 +25,12 @@
options.UseSqlite("Data Source=BiwenSettings.db");
});

builder.Services.AddHttpLogging(options =>
{
options.LoggingFields = HttpLoggingFields.All;
options.CombineLogs = true;
});


//配置garnet client
builder.Services.Configure<GarnetClientOptions>(options =>
Expand Down Expand Up @@ -101,7 +109,12 @@
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
if (app.Environment.IsDevelopment())
{
app.UseHttpLogging();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
Expand All @@ -113,10 +126,11 @@

app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseRouting();

app.MapRazorPages();

app.UseBiwenSettings(mapNotifyEndpoint: true, builder: builder =>
Expand Down
2 changes: 1 addition & 1 deletion Biwen.Settings.TestWebUI/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Microsoft.AspNetCore": "Information"
}
}
}
19 changes: 19 additions & 0 deletions Biwen.Settings/Biwen.Settings.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@Biwen.Settings_HostAddress = http://localhost:5150

POST {{Biwen.Settings_HostAddress}}/biwensetting/api/set/Biwen.Settings.TestWebUI.GithubSetting
Accept: application/json
Content-Type: application/json-patch+json
{
"UserName" : "vipwan",
"Token":"234567"
}

###

GET {{Biwen.Settings_HostAddress}}/biwensetting/api/all

###

GET {{Biwen.Settings_HostAddress}}/biwensetting/api/get/Biwen.Settings.TestWebUI.GithubSetting

###
17 changes: 8 additions & 9 deletions Biwen.Settings/Mvc/ValidDtoFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System.Dynamic;

namespace Biwen.Settings.Mvc
Expand All @@ -10,7 +11,8 @@ internal class ValidDtoFilter : IEndpointFilter
{
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
var id = context.Arguments[3]!.ToString(); //context.HttpContext.GetRouteValue("id") as string;
var id = context.HttpContext.GetRouteData().Values["id"] as string;

if (string.IsNullOrEmpty(id)) return Results.NotFound();
var type = ASS.InAllRequiredAssemblies.FirstOrDefault(x => x.FullName == id);
if (type == null) return Results.NotFound();
Expand Down Expand Up @@ -50,8 +52,9 @@ internal class ValidDtoFilter : IEndpointFilter
//赋值
prop.SetValue(setting, value);
}
var option = (context.Arguments[1] as IOptions<SettingOptions>)!.Value;
if (option.AutoFluentValidationOption.Enable)

var option = context.HttpContext.RequestServices.GetRequiredService(typeof(IOptions<SettingOptions>)) as IOptions<SettingOptions>;
if (option!.Value.AutoFluentValidationOption.Enable)
{
//继承至ValidationSettingBase<T>的情况
if (type.BaseType!.IsConstructedGenericType && type.BaseType!.GenericTypeArguments.Any(x => x == type))
Expand Down Expand Up @@ -79,13 +82,9 @@ internal class ValidDtoFilter : IEndpointFilter

//存在验证器的情况
var validator = context.HttpContext!.RequestServices.GetService(typeof(IValidator<>).MakeGenericType(type));
if (validator != null)
if (validator is not null && Valid(validator) is (false, var errors))
{
var (Succesed, Errors) = Valid(validator);
if (!Succesed)
{
return Results.ValidationProblem(Errors!);
}
return Results.ValidationProblem(errors!);
}
}
return await next(context);
Expand Down
2 changes: 1 addition & 1 deletion Version.Props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<PackageVersion>2.2.6.1</PackageVersion>
<PackageVersion>2.2.6.2</PackageVersion>
<PackageReleaseNotes>提供对IConfiguration和IOptions的集成支持</PackageReleaseNotes>
<AssemblyVersion>2.2.0</AssemblyVersion>
<FileVersion>2.2.0</FileVersion>
Expand Down

0 comments on commit 8dda8e2

Please sign in to comment.