- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个捕获错误并记录错误的过滤器,但是我希望它返回正确的 HttpStatusCode ,以便我的客户端应用程序可以显示不错的错误消息。
当前,在我的过滤器属性的重写的 OnException 方法中,我尝试通过将异常转换为 HttpException 来获取状态码。
看起来像这样:
public class LogExceptionFilterAttribute : ExceptionFilterAttribute
{
// Private properties
private readonly ILogProvider _logger;
/// <summary>
/// Our default constructor
/// </summary>
/// <param name="logger"></param>
public LogExceptionFilterAttribute(ILogProvider logger)
{
_logger = logger;
}
/// <summary>
/// Invoked when an exception has been thrown
/// </summary>
/// <param name="context">The context</param>
public override async void OnException(HttpActionExecutedContext context)
{
// Get our user
var requestContext = context.Request.GetRequestContext();
var user = requestContext.Principal.Identity;
// Create our response
var message = await _logger.ErrorAsync(context.Exception, user);
var content = new HttpResponseMessage
{
Content = new StringContent(message),
StatusCode = GetStatusCodeFromException(context)
};
// Assign our response to our context
context.Response = content;
}
/// <summary>
/// Gets a status code from an error
/// </summary>
/// <param name="context">The context</param>
/// <returns></returns>
private static HttpStatusCode GetStatusCodeFromException(HttpActionExecutedContext context)
{
// Cast the exception as an HttpException
var exception = context.Exception as HttpException;
// If we don't have an exception
if (exception != null)
{
// Return the HttpException code
return (HttpStatusCode)exception.GetHttpCode();
}
// Return Internal Server Error
return HttpStatusCode.InternalServerError;
}
}
private static HttpStatusCode GetStatusCodeFromException(HttpActionExecutedContext context)
{
// Cast the exception as an HttpException
var exception = context.Exception as HttpException;
// If there is still an exception, return the code
if (exception != null)
return (HttpStatusCode)exception.GetHttpCode();
// Otherwise, work out what type of error we have
switch(context.Exception.GetType().ToString())
{
case "NullReferenceException":
return HttpStatusCode.BadRequest;
default:
return HttpStatusCode.InternalServerError;
}
}
最佳答案
与其在名称上比较类型,不如实际检查类型本身要安全得多:
if (context.Exception is NullReferenceException)
{
return HttpStatusCode.BadProgrammer;
}
else
{
return HttpStatusCode.InternalServerError;
}
关于c# - 使用FilterAttributes的Web API异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228123/
我在 MVC 应用程序的 Controller 中使用了 2 个属性。 AttributeA 扩展 System.Web.Mvc.AuthorizeAttribute和 AttributeB 扩展 S
我的请求示例 http://localhost:8065/api/note POST content-type:application/json request body: { "id" : "123
我正在编写一个授权过滤器属性,但我无法弄清楚如何以字符串形式获取当前 url,以便我可以将其作为参数传递给 LogOn 操作。目标是,如果用户成功登录,他们将被重定向到他们最初尝试访问的页面。 pub
我想这样做: [RequiresAuthentication(CompanyType.Client)] public class FooController { public Acti
这个问题在这里已经有了答案: Using Unity to inject dependencies into a custom ActionFilter (1 个回答) 关闭 7 年前。 我已经在我
我有一个定制HandleErrorAttribute扩展 FilterAttribute . 如何让 Unity 在注入(inject) Controller 依赖项本身的同时将依赖项注入(injec
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我对 HTTPS 和 HTTP 有疑问。看来我的过滤器属性正在被缓存。当我登录时,它应该自动将无 http 页面发送到 https,它确实这样做了。然后,当我注销时,它会保持在 https 上,这很好
我在 ASP.NET MVC 6(beta 4 版本)中遇到困难,试图在 AuthorizationFilterAttribute 类型的 Controller 过滤器属性中注入(inject)服务。
我有一个自定义的 ExceptionFilter,用于记录 Web-Api Controller 内所有未捕获的异常。我想使用 Autofac,向其注入(inject) ILog 配置。 我的问题是如
我有一个简单的自定义 FilterAttribute,我用它装饰各种 ActionMethods。 例如。 [AcceptVerbs(HttpVerbs.Get)] [MyCustomFilter]
我正在为我的 MVC 项目编写自定义 ErrorHandler 属性。我想将 EventViewerLogger 的实现注入(inject)到该属性中。 我正在使用 Ninject 2.2,它对于其他
我是一名优秀的程序员,十分优秀!