Mik*_*ynn 8 asp.net-mvc-4 asp.net-web-api
如果服务层未找到请求的资源,则返回null到Web API控制器; 什么是将HttpStatusCode.NotFound响应返回给客户端而不在控制器中对其进行硬编码,以及检查它是否为空的最佳方法?
Mar*_*nes 15
就个人而言,我只是根据Oppositional的评论对控制器进行检查,但你要求的是完全合理的.再次使用每个控制器附加的动作过滤器(或全局注册),你可以沿着这些方向做一些事情:
示例型号:
public class Foo
{
public string Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
示例控制器:
public class FoosController : ApiController
{
[NullObjectActionFilter]
public Foo Get(string id)
{
// - Returns model and 200
//return new Foo() { Bar = "TEST" };
// - Returns 404
//return null;
}
}
Run Code Online (Sandbox Code Playgroud)
过滤器:
public class NullObjectActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
object outValue = null;
actionExecutedContext.Response.TryGetContentValue<object>(out outValue);
if (outValue == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
base.OnActionExecuted(actionExecutedContext);
}
}
Run Code Online (Sandbox Code Playgroud)
cri*_*mbo 11
我同意马克认为ActionFilter是可行的方法 - 小动作方法是一种很好的气味.
但是,HttpActionExecutedContext.Response可以null在发生异常时; 并且上面显示的NullObjectActionFilter类可能会掩盖错误的HTTP状态代码.您最好检查成功退出和成功的HTTP代码.
这是我使用的动作过滤器:
/// <summary>
/// Converts <c>null</c> return values into an HTTP 404 return code.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NullResponseIs404Attribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if ((actionExecutedContext.Response != null) && actionExecutedContext.Response.IsSuccessStatusCode)
{
object contentValue = null;
actionExecutedContext.Response.TryGetContentValue<object>(out contentValue);
if (contentValue == null)
{
actionExecutedContext.Response = actionExecutedContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "Object not found");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6875 次 |
| 最近记录: |