找到路由/URL却找不到其背后的资源时返回什么? [英] What to return when a route/url is found but not the resource behind it?
问题描述
如果存在路径customer/1,但不存在客户搜索背后的资源/实体,则
我应该返回404吗?我的意思是路线存在...
或者我应该返回一个
204(无内容),因为我找不到客户,结果为空.
Microsoft示例:
public IHttpActionResult Get (int id)
{
Product product = _repository.Get (id);
if (product == null)
{
return NotFound(); // Returns a NotFoundResult
}
return Ok(product); // Returns an OkNegotiatedContentResult
}
我认为NotFound()=> 404在这里不正确?!
我的问题的澄清/改进.
我需要区分
1)路线:客户/1 =>路线不存在
(我会在这里返回404,但这还是由框架处理的...)
2)路线:客户/1 =>存在,但数据库中的资源/实体不是
(我会在这里返回404)
3)路线:customersSearch/searchterm =>找不到数据库中的资源/实体
(我会返回204)
请问您是那样的人,请更正对这个更明确的问题的答案吗?
常规REST准则指出,在这种情况下,您应该返回404.您要求的特定资源不存在.好像您是从网站上请求实物文件一样.
在我所有的工作场所中,模式都是由企业体系结构团队发布的:)
一个建议,尽管...对于这种情况有某种形式的日志记录,因此您可以检测到逻辑" 404(在这种情况下),而不是真正的没有资源/处理程序/脚本映射可用于服务请求!至少要在部署时确定问题:)When a route customer/1 exists but the resource/entity behind the customer search does not exist,
Should I return a 404? I mean the route exists...
or should I return a
204 (No content) because I could not find a customer and the result is empty.
Microsoft sample:
public IHttpActionResult Get (int id)
{
Product product = _repository.Get (id);
if (product == null)
{
return NotFound(); // Returns a NotFoundResult
}
return Ok(product); // Returns an OkNegotiatedContentResult
}
In my opinion a NotFound() => 404 is not correct here?!
CLARIFICATION/IMPROVEMENT of my question.
I need to distinguish between a
1) route: customer/1 => route does not exist
( I would return here 404, but this is anyway handled by the framework...)
2) route: customer/1 => exists but the resource/entity in database NOT
( I would return here a 404)
3) route: customersSearch/searchterm => the resource/entity in database is not found
( I would return a 204 )
Would you please be that kind and correct your answer to this more clear question please?
General REST guidelines state that in this instance you should return a 404. You have asked for a specific resource that doesn't exist. As if you had requested a physical document from a website.
At all my work places that has been the pattern and issued by the enterprise architecture teams :)
A word of advice though... have some form of logging for this scenario so you can detect a "logical" 404 (in this scenario) versus a true no resource / handler / script mapping was available to service the request! At least to identify issues at deploy time :)
这篇关于找到路由/URL却找不到其背后的资源时返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!