[英]ASP.NET Web Api: Return user friendly names in error response from ActionFilter/ModelState
I'm using a view model with an ApiController that has Display(Name = "Friendly Name") attributes for each property. 我正在使用具有ApiController的视图模型,该模型具有每个属性的Display(Name =“ Friendly Name”)属性。 I've created a custom ActionFilterAttribute
to validate the model state and return a bad request when the model is not valid. 我创建了一个自定义ActionFilterAttribute
以验证模型状态并在模型无效时返回错误请求。 What I would like to do now is return custom JSON output that includes the user friendly names related to each model's properties. 我现在想做的是返回自定义JSON输出,其中包括与每个模型的属性相关的用户友好名称。 I can think of some hacky ways to do this but what's the preferred way? 我可以想到一些骇人听闻的方法来做到这一点,但是首选的方法是什么?
EDIT: This will only be fixed in WebApi 5.1. 编辑:这只会在WebApi 5.1中修复。
Have a look here - Web Api ModelState validation is ignoring the DisplayAttribute 在这里看看-Web Api ModelState验证忽略了DisplayAttribute
Have a look at the official Microsoft documentation - ApiController.Validate . 看看Microsoft的官方文档-ApiController.Validate 。
You can achieve this by returning the ModelState in your response. 您可以通过在响应中返回ModelState来实现。 eg 例如
public class MyController : ApiController
{
public HttpResponseMessage Post(Product product)
{
if (ModelState.IsValid)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
return request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
}
or if you want to do this through your ActionFilterAttribute you can use: 或者,如果您想通过ActionFilterAttribute执行此操作,则可以使用:
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
This would return JSON like: 这将返回JSON,例如:
{ "Message": "The request is invalid.", "ModelState": { "product": [ "Required property 'Name' not found in JSON. Path '', line 1, position 17." {“ Message”:“请求无效。”,“ ModelState”:{“ product”:[“在JSON中找不到必需的属性'Name'。路径,第1行,位置17。” ], "product.Name": [ "The Name field is required." ],“ product.Name”:[“名称字段为必填。” ], "product.Weight": [ "The field Weight must be between 0 and 999." ],“ product.Weight”:[“该字段的重量必须在0到999之间。” ] } } ]}}
In 'ActionFilterAttribute' you can can customize error message according your api requirement 在“ ActionFilterAttribute”中,您可以根据自己的API要求自定义错误消息
public class ValidateModelAttribute : ActionFilterAttribute{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
var error = new
{
status = false,
message = "The request is invalid.",
error = actionContext.ModelState.Values.SelectMany(e => e.Errors.Select(er => er.ErrorMessage))
};
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, error);
}
}}
{ "status": false, "message": "The request is invalid.", "error": [ "The First Name field is required.", "The Lname field is required.", "The Ename field is required." {“ status”:否,“ message”:“请求无效。”,“ error”:[“ First Name字段为必填。”,“ Lname字段为必填。”,“ Ename字段为必填。 “ ], } ],}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.