我正在尝试在web api控制器中返回未针对GET方法修改的304的状态代码。
我成功的唯一方法就是这样:
public class TryController : ApiController
{
public User GetUser(int userId, DateTime lastModifiedAtClient)
{
var user = new DataEntities().Users.First(p => p.Id == userId);
if (user.LastModified <= lastModifiedAtClient)
{
throw new HttpResponseException(HttpStatusCode.NotModified);
}
return user;
}
}
这里的问题是它不是一个例外,它只是没有修改,所以客户端缓存是好的。 我还希望返回类型是User(因为所有web api示例都显示为GET)不返回HttpResponseMessage或类似的东西。
答案 0 :(得分:232)
我不知道答案所以要求ASP.NET团队here。
所以诀窍是将签名更改为HttpResponseMessage
并使用Request.CreateResponse
。
[ResponseType(typeof(User))]
public HttpResponseMessage GetUser(HttpRequestMessage request, int userId, DateTime lastModifiedAtClient)
{
var user = new DataEntities().Users.First(p => p.Id == userId);
if (user.LastModified <= lastModifiedAtClient)
{
return new HttpResponseMessage(HttpStatusCode.NotModified);
}
return request.CreateResponse(HttpStatusCode.OK, user);
}
答案 1 :(得分:66)
如果要将操作签名保留为返回用户:
,也可以执行以下操作public User GetUser(int userId, DateTime lastModifiedAtClient)
如果您想要返回200
以外的其他内容,则会在您的操作中抛出HttpResponseException
并传递您要发送给客户的HttpResponseMessage
。
答案 2 :(得分:39)
更改GetXxx API方法以返回HttpResponseMessage,然后返回完整响应的类型版本和NotModified响应的非类型化版本。
public HttpResponseMessage GetComputingDevice(string id)
{
ComputingDevice computingDevice =
_db.Devices.OfType<ComputingDevice>()
.SingleOrDefault(c => c.AssetId == id);
if (computingDevice == null)
{
return this.Request.CreateResponse(HttpStatusCode.NotFound);
}
if (this.Request.ClientHasStaleData(computingDevice.ModifiedDate))
{
return this.Request.CreateResponse<ComputingDevice>(
HttpStatusCode.OK, computingDevice);
}
else
{
return this.Request.CreateResponse(HttpStatusCode.NotModified);
}
}
* ClientHasStale数据是我检查ETag和IfModifiedSince标题的扩展名。
MVC框架仍应序列化并返回您的对象。
注意强>
我认为在未来的Web API版本中将删除通用版本。
答案 3 :(得分:36)
在MVC 5中,事情变得更容易:
return new StatusCodeResult(HttpStatusCode.NotModified, this);
答案 4 :(得分:11)
我讨厌碰到旧文章,但这是谷歌搜索中的第一个结果,我对这个问题有一点时间(即使有你们的支持)。所以这里什么都没有......
希望我的解决方案能够帮助那些也感到困惑的人。
namespace MyApplication.WebAPI.Controllers
{
public class BaseController : ApiController
{
public T SendResponse<T>(T response, HttpStatusCode statusCode = HttpStatusCode.OK)
{
if (statusCode != HttpStatusCode.OK)
{
// leave it up to microsoft to make this way more complicated than it needs to be
// seriously i used to be able to just set the status and leave it at that but nooo... now
// i need to throw an exception
var badResponse =
new HttpResponseMessage(statusCode)
{
Content = new StringContent(JsonConvert.SerializeObject(response), Encoding.UTF8, "application/json")
};
throw new HttpResponseException(badResponse);
}
return response;
}
}
}
然后继承BaseController
[RoutePrefix("api/devicemanagement")]
public class DeviceManagementController : BaseController
{...
然后使用它
[HttpGet]
[Route("device/search/{property}/{value}")]
public SearchForDeviceResponse SearchForDevice(string property, string value)
{
//todo: limit search property here?
var response = new SearchForDeviceResponse();
var results = _deviceManagementBusiness.SearchForDevices(property, value);
response.Success = true;
response.Data = results;
var statusCode = results == null || !results.Any() ? HttpStatusCode.NoContent : HttpStatusCode.OK;
return SendResponse(response, statusCode);
}
答案 5 :(得分:5)
.net核心2.2返回304状态代码。这是使用ApiController。
[HttpGet]
public ActionResult<YOUROBJECT> Get()
{
return StatusCode(304);
}
您可以选择返回带有响应的对象
[HttpGet]
public ActionResult<YOUROBJECT> Get()
{
return StatusCode(304, YOUROBJECT);
}
答案 6 :(得分:2)
public HttpResponseMessage Post(Article article)
{
HttpResponseMessage response = Request.CreateResponse<Article>(HttpStatusCode.Created, article);
string uriToTheCreatedItem = Url.Route(null, new { id = article.Id });
response.Headers.Location = new Uri(Request.RequestUri, uriToTheCreatedItem);
return response;
}
答案 7 :(得分:2)
如果您需要返回IHttpActionResult并想要返回错误代码和消息,请使用:
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotModified, "Error message here"));
答案 8 :(得分:2)
对于ASP.NET Web Api 2,this post from MS建议将方法的返回类型更改为IHttpActionResult
。然后,您可以返回内置的IHttpActionResult
实现,例如Ok
,BadRequest
等(see here),也可以返回自己的实现。
对于您的代码,可以这样完成:
public IHttpActionResult GetUser(int userId, DateTime lastModifiedAtClient)
{
var user = new DataEntities().Users.First(p => p.Id == userId);
if (user.LastModified <= lastModifiedAtClient)
{
return StatusCode(HttpStatusCode.NotModified);
}
return Ok(user);
}
答案 9 :(得分:1)
另一种选择:
return new NotModified();
public class NotModified : IHttpActionResult
{
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage(HttpStatusCode.NotModified);
return Task.FromResult(response);
}
}
答案 10 :(得分:1)
我不想更改我的签名以使用HttpCreateResponse类型,因此我提出了一些扩展解决方案来隐藏它。
ALTER TABLE `lc_error_logs` MODIFY `OneDetailedMessage` VARCHAR(5000) NULL DEFAULT NULL;
然后,您可以向ApiController(或更好的基本控制器)添加一个方法,如下所示:
public class HttpActionResult : IHttpActionResult
{
public HttpActionResult(HttpRequestMessage request) : this(request, HttpStatusCode.OK)
{
}
public HttpActionResult(HttpRequestMessage request, HttpStatusCode code) : this(request, code, null)
{
}
public HttpActionResult(HttpRequestMessage request, HttpStatusCode code, object result)
{
Request = request;
Code = code;
Result = result;
}
public HttpRequestMessage Request { get; }
public HttpStatusCode Code { get; }
public object Result { get; }
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Request.CreateResponse(Code, Result));
}
}
然后你可以像任何内置方法一样返回它:
protected IHttpActionResult CustomResult(HttpStatusCode code, object data)
{
// Request here is the property on the controller.
return new HttpActionResult(Request, code, data);
}
答案 11 :(得分:0)
使用Web API 2中引入的更现代的IHttpActionResult
来更新@Aliostads答案。
public class TryController : ApiController
{
public IHttpActionResult GetUser(int userId, DateTime lastModifiedAtClient)
{
var user = new DataEntities().Users.First(p => p.Id == userId);
if (user.LastModified <= lastModifiedAtClient)
{
return StatusCode(HttpStatusCode.NotModified);
// If you would like to return a Http Status code with any object instead:
// return Content(HttpStatusCode.InternalServerError, "My Message");
}
return Ok(user);
}
}
答案 12 :(得分:0)
尝试一下:
return new ContentResult() {
StatusCode = 404,
Content = "Not found"
};
答案 13 :(得分:0)
我知道这里有几个不错的答案,但这正是我所需要的,所以我想我会添加此代码,以防其他人需要使用 webAPI 在 4.7.x 中返回他们想要的任何状态代码和响应正文。>
public class DuplicateResponseResult<TResponse> : IHttpActionResult
{
private TResponse _response;
private HttpStatusCode _statusCode;
private HttpRequestMessage _httpRequestMessage;
public DuplicateResponseResult(HttpRequestMessage httpRequestMessage, TResponse response, HttpStatusCode statusCode)
{
_httpRequestMessage = httpRequestMessage;
_response = response;
_statusCode = statusCode;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage(_statusCode);
return Task.FromResult(_httpRequestMessage.CreateResponse(_statusCode, _response));
}
}