How to send custom object with modelState, or expand modelState object

跟風遠走 提交于 2019-12-11 03:59:35

问题


This is a follow-up question from How to send a custom object from OnException method?

This is my current code for OnActionExecuting:

public override void OnActionExecuting(HttpActionContext actionContext)
{
   var modelState = actionContext.ModelState;

    if (!modelState.IsValid)
        actionContext.Response = actionContext.Request
             .CreateErrorResponse(HttpStatusCode.BadRequest, modelState);
}

All my WebAPI requests use this filter which checks whether the model is valid or not.

If not, it will send the modelState object (which works fine).

What I want is this: I want to send this modelState + my Custom ErrorMsgModel shown below in the response:

public class ErrorModel
{
    public string StatusMsg { get; set; }
    public int StatusCode { get; set; }
}

Let's create an object of the ErrorModel class:

ErrorModel StatusModel = new ErrorModel();
StatusModel.StatusCode = 500;
StatusModel.StatusMsg = "My Custom Msg";

So finally, I want to send this object back. Something like:

//return modelState + StatusModel;

Or just append the integer StatusCode manually to modelState object.

How to do that?

来源:https://stackoverflow.com/questions/30622911/how-to-send-custom-object-with-modelstate-or-expand-modelstate-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!