C# 如何将特定于应用程序的异常直接映射到Http异常?
我正在开发一个ASP.NETMVC3应用程序。在我们应用程序的所谓“业务层”中,我们决定根据情况始终抛出一些特定类型的异常。我们有一个异常类型层次结构,当用户试图做一些他未被授权的事情时,以及当应用程序找不到给定项(通过Id或名称或其他方式)时的特殊异常 这在我们的C代码中看起来是这样的: 。。。其中C# 如何将特定于应用程序的异常直接映射到Http异常?,c#,asp.net-mvc,action-filter,custom-error-pages,handleerror,C#,Asp.net Mvc,Action Filter,Custom Error Pages,Handleerror,我正在开发一个ASP.NETMVC3应用程序。在我们应用程序的所谓“业务层”中,我们决定根据情况始终抛出一些特定类型的异常。我们有一个异常类型层次结构,当用户试图做一些他未被授权的事情时,以及当应用程序找不到给定项(通过Id或名称或其他方式)时的特殊异常 这在我们的C代码中看起来是这样的: 。。。其中SomethingNotFoundException和NotAuthorizedException是自定义的Exceptions 在这类异常和Http状态代码(404未找到/403禁止)之间有一个直
SomethingNotFoundException
和NotAuthorizedException
是自定义的Exception
s
在这类异常和Http状态代码(404未找到/403禁止)之间有一个直接映射,我们希望我们的控制器方法能够相应地处理这些错误(显示404/403 CustomError页面等等)。现在,我们想要的是避免在控制器操作中执行此操作:
public ViewResult Get(int id){
try{
var theSomething = MyService.GetSomething(id, theUser);
}
catch(SomethingNotFoundException ex){
throw new HttpException(404, ex);
}
catch(NotAuthorizedExceptionex){
throw new HttpException(403, ex);
}
}
我确信一定有办法使用自定义HandleErrorAttribute或自定义ActionFilterAttribute并将其注册到Global.asax中,但我不知道如何让它工作
尝试1:HandleErrorAttribute
我首先尝试创建HandleErrorAttribute的子类,重写OneException
方法如下:
public override void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
Exception exception = filterContext.Exception;
// Map some of the Business Exception to correspounding HttpExceptions !
if (exception is ObjectNotFoundException)
{
// consider it as a NotFoundException !
exception = new HttpException((int)HttpStatusCode.NotFound, "Not found", exception);
filterContext.Exception = exception;
}
else if (exception is NotAuthorizedException)
{
// consider it as a ForbiddenException
exception = new HttpException((int)HttpStatusCode.Forbidden, "Forbidden", exception);
filterContext.Exception = exception;
}
base.OnException(filterContext);
}
。。。并将其添加到Global.asax中的GlobalFilterCollection
。。。但它仍然被当作通常的500错误处理,而不是显示404/403自定义错误页面
尝试2:ActionFilterAttribute
我还尝试将其设置为actionfilteratAttribute
,并覆盖OnActionExecuted
方法,如下所示:
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
Exception exception = filterContext.Exception;
if(exception!=null)
{
// Map some of the Business Exception to correspounding HttpExceptions !
if (exception is ObjectNotFoundException)
{
// consider it as a NotFoundException !
var wrappingException = new HttpException((int)HttpStatusCode.NotFound, "Not found", exception);
exception = wrappingException;
filterContext.Exception = exception;
}
else if (exception is NotAuthorizedException)
{
// consider it as a ForbiddenException
var wrappingException = new HttpException((int)HttpStatusCode.Forbidden, "Forbidden", exception);
exception = wrappingException;
filterContext.Exception = exception;
}
}
base.OnActionExecuted(filterContext);
}
。。。但是,我还是得到了500错误页面,而不是404或403
我做错什么了吗?有没有更好的办法?嗯,有几个选择 您可能想要:解决方案 你可以这样做 您可以拥有一个所有类都继承自的基本控制器类,并使用以下方法。我的上一家公司就是这样解决这个问题的
protected override void OnException(ExceptionContext filterContext)
{
Console.WriteLine(filterContext.Exception.ToString());
}
嗯,有几个选择 您可能想要:解决方案 你可以这样做 您可以拥有一个所有类都继承自的基本控制器类,并使用以下方法。我的上一家公司就是这样解决这个问题的
protected override void OnException(ExceptionContext filterContext)
{
Console.WriteLine(filterContext.Exception.ToString());
}