验证错误的正确返回类型

时间:2015-06-30 07:41:29

标签: c# .net asp.net-mvc asp.net-web-api

我需要在我的web api控制器中实现验证。 在我的班上,我有一个这样的方法:

<meta name="apple-mobile-web-app-capable" content="no">

在POST和PUT方法中,我通常返回创建/更新的对象。

this tutoral中,他们返回public MyEntity Post(MyEntity entity) { // ... } ,以便他们可以执行以下操作:

HttpResponseMessage

有没有办法使用类似的方法返回已保存的实体?

1 个答案:

答案 0 :(得分:2)

您应该使用HttpRequestMessageExtensions.CreateResponse方法。例如:

if (ModelState.IsValid)
{
    // Do something with the product (not shown).

    return Request.CreateResponse<MyEntity>(HttpStatusCode.OK, entity);
}