问题
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