🚀 feat(新增): 表单验证整合返回格式
This commit is contained in:
parent
f00cd37feb
commit
86e879b288
|
@ -2,7 +2,19 @@
|
|||
<project version="4">
|
||||
<component name="GitCommitMessageStorage">
|
||||
<option name="messageStorage">
|
||||
<MessageStorage />
|
||||
<MessageStorage>
|
||||
<option name="commitTemplate">
|
||||
<CommitTemplate>
|
||||
<option name="body" value="" />
|
||||
<option name="changes" value="" />
|
||||
<option name="closes" value="" />
|
||||
<option name="scope" value="" />
|
||||
<option name="skipCi" value="" />
|
||||
<option name="subject" value="" />
|
||||
<option name="type" value="feat" />
|
||||
</CommitTemplate>
|
||||
</option>
|
||||
</MessageStorage>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
|
@ -2,7 +2,6 @@
|
|||
using Bunny.Dao.Model.Result;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace Bunny.Common.Filter;
|
||||
|
||||
|
@ -16,9 +15,11 @@ public class ValidateModelStateAttribute : ActionFilterAttribute
|
|||
if (actionContext.ModelState.IsValid) return;
|
||||
|
||||
// 表单验证失败
|
||||
var errorMessage = actionContext.ModelState.Values.First().Errors.First().ErrorMessage;
|
||||
var result =
|
||||
Result<ModelStateDictionary>.Error(HttpStatusCode.BadRequest, actionContext.ModelState, errorMessage);
|
||||
var errorMessage = string.Join(",", actionContext.ModelState.Values
|
||||
.SelectMany(value => value.Errors)
|
||||
.Select(value => value.ErrorMessage));
|
||||
|
||||
var result = Result<string>.Error(HttpStatusCode.BadRequest, errorMessage);
|
||||
|
||||
// 重新复写返回结果
|
||||
actionContext.Result = new ObjectResult(result);
|
||||
|
|
Loading…
Reference in New Issue