Skip to content

Commit

Permalink
#14
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Oct 18, 2023
1 parent 43e256d commit b5eeb11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Biwen.QuickApi.DemoWeb/Apis/HelloApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public class HelloApiRequest : AuthRequest<HelloApiRequest>
[FromQuery(Name = "member")]
public Member CurrentMember { get; set; }

[Description("header比如{\"id\":\"123\",\"userName\":\"vipwan\"}")]
[FromHeader(Name = "headmember")]
public Member CurrentMemberFromHeader { get; set; }


public record Member(string Id,string UserName);

Expand All @@ -77,6 +81,9 @@ public HelloApiRequest()
RuleFor(x => x.Name).NotNull().Length(2, 36);
RuleFor(x => x.Password).NotNull().Length(2, 36);
RuleFor(x => x.UserName).EmailAddress();//要求邮箱
//嵌套对象验证
RuleFor(x=>x.CurrentMember.UserName).EmailAddress();
RuleFor(x=>x.CurrentMemberFromHeader.UserName).EmailAddress();
}
}

Expand Down
11 changes: 4 additions & 7 deletions Biwen.QuickApi/IReqBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public virtual async Task<T> BindAsync(HttpContext context)
{
var name = fromHeader.Name ?? prop.Name;
var qs = context.Request.Headers;
if (qs.TryGetValue(name, out Microsoft.Extensions.Primitives.StringValues val))
if (qs.TryGetValue(name, out StringValues val))
{
//转换
var value = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromInvariantString(val.ToString());
var value = StringValuesExtensions.DeserializeJsonArrayString(val, prop.PropertyType);
prop.SetValue(@default, value);
continue;
}
Expand Down Expand Up @@ -366,7 +366,6 @@ static class StringValuesExtensions
PropertyNameCaseInsensitive = true
};


/// <summary>
/// 将QueryString转换为数组
/// </summary>
Expand All @@ -377,7 +376,6 @@ static class StringValuesExtensions
{
if (input is not StringValues vals || vals.Count == 0)
return null;

if (vals.Count == 1 && vals[0]!.StartsWith('[') && vals[0]!.EndsWith(']'))
{
// querystring: ?ids=[1,2,3]
Expand All @@ -388,13 +386,12 @@ static class StringValuesExtensions

return JsonSerializer.Deserialize(vals[0]!, tProp, _serializerOptions);
}

//如果是单个对象且不是字符串,则直接返回
//?user={"id":"123","name":"x"}
if (tProp != typeof(string) && !tProp.GetInterfaces().Any(x => x == typeof(IEnumerable)))
{
return JsonSerializer.Deserialize(vals[0]!, tProp, _serializerOptions);
}

}
// querystring: ?ids=one&ids=two
// possible inputs:
// - 1 (as StringValues)
Expand Down

0 comments on commit b5eeb11

Please sign in to comment.