You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.7 KiB
49 lines
1.7 KiB
2 years ago
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||
|
using Newtonsoft.Json;
|
||
|
using Znyc.CloudCar.Model.ViewModels.Basics;
|
||
|
using Znyc.CloudCar.Model.ViewModels.ReportsCallBack;
|
||
|
|
||
|
namespace Znyc.CloudCar.Filter
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 请求验证错误处理
|
||
|
/// </summary>
|
||
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true)]
|
||
|
public class RequiredError : ResultFilterAttribute
|
||
|
{
|
||
|
public override void OnResultExecuting(ResultExecutingContext context)
|
||
|
{
|
||
|
var modelState = context.ModelState;
|
||
|
List<ErrorView> errors = new List<ErrorView>();
|
||
|
if (!modelState.IsValid)
|
||
|
{
|
||
|
var baseResult = new ResponseOutput()
|
||
|
{
|
||
|
Successed = false,
|
||
|
Msg = "请提交必要的参数",
|
||
|
};
|
||
|
foreach (var key in modelState.Keys)
|
||
|
{
|
||
|
var state = modelState[key];
|
||
|
if (state.Errors.Any())
|
||
|
{
|
||
|
ErrorView errorView = new ErrorView();
|
||
|
errorView.ErrorName = key;
|
||
|
errorView.Error = state.Errors.First().ErrorMessage;
|
||
|
errors.Add(errorView);
|
||
|
}
|
||
|
}
|
||
|
ResponseOutput response = new ResponseOutput();
|
||
|
response.Data = errors;
|
||
|
response.Successed = false;
|
||
|
context.Result = new ContentResult()
|
||
|
{
|
||
|
Content = JsonConvert.SerializeObject(response),
|
||
|
ContentType = "application/json"
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|