抛出HttpException总是返回HTTP 500错误
本文关键字:HTTP 错误 返回 HttpException 抛出 | 更新日期: 2023-09-27 17:49:42
我正试图向客户端抛出HTTP 403错误代码。我读到HttpException是最干净的方式来完成这一点,但它不适合我。我从页面内抛出异常,如下所示:
throw new HttpException(403,"You must be logged in to access this resource.");
然而,这只会给出一个标准的ASP。当CustomErrors关闭时,Net堆栈跟踪(带有500个错误)。如果CustomErrors打开,那么当发生403错误时,它不会重定向到我设置的要显示的页面。我应该忘记HttpException,而是自己设置所有的HTTP代码吗?我该如何解决这个问题?
Web的自定义错误部分。配置如下:
<customErrors mode="On" defaultRedirect="GenericErrorPage.html">
<error statusCode="403" redirect="Forbidden.html" />
</customErrors>
不是得到Forbidden.html,而是得到GenericErrorPage.html
您需要像这样覆盖应用程序错误:
protected void Application_Error()
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
Response.StatusCode = 500;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 403:
routeData.Values["action"] = "Http403";
break;
case 404:
routeData.Values["action"] = "Http404";
break;
}
}
IController errorsController = new ErrorsController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
然后添加errorsController:
public class ErrorsController : Controller
{
public ActionResult General(Exception exception)
{
ViewBag.ErrorCode = Response.StatusCode;
ViewBag.Message = "Error Happened";
//you should log your exception here
return View("Index");
}
public ActionResult Http404()
{
ViewBag.ErrorCode = Response.StatusCode;
ViewBag.Message = "Not Found";
return View("Index");
}
public ActionResult Http403()
{
ViewBag.Message = Response.StatusCode;
ViewBag.Message = "Forbidden";
return View("Index");
}
}
最后为errorsController创建一个视图。我只在Views/Errors/中创建了一个名为index的视图
这个代码包含在元素configuration/system中。web of web。配置文件:
<customErrors mode="On">
<error statusCode="403" redirect="~/errors/Forbidden.aspx"/>
</customErrors>
我设法使它按预期工作。
你可以在这里找到一个很好的示例教程(示例3是正确的):http://aspnetresources.com/articles/CustomErrorPages
或者您可以使用Response。这样做的状态:Asp Classic返回特定的http状态码
我实际上已经创建了我自己的漂亮的小类来解决这个问题。它不能处理一切,我不确定它与MVC很好,但它适用于我的使用。基本上,不依赖于ASP。Net输出正确的错误页面和错误代码,它将清除错误,然后执行服务器端传输并显示适当的Web。配置错误页面。它还可以识别customErrors模式并做出相应的反应。
public static class CustomErrorsFixer
{
static public void HandleErrors(HttpContext Context)
{
if(RunIt(Context)==false){
return;
}
HttpException ex = Context.Error as HttpException;
if(ex==null){
try{
ex=Context.Error.InnerException as HttpException;
}catch{
ex=null;
}
}
if (ex != null){
Context.Response.StatusCode = ex.GetHttpCode();
}else{
Context.Response.StatusCode = 500;
}
Context.ClearError();
Context.Server.Transfer(GetCustomError(Context.Response.StatusCode.ToString()));
HttpContext.Current.Response.End();
}
static protected string GetCustomError(string code)
{
CustomErrorsSection section = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
if (section != null)
{
CustomError page = section.Errors[code];
if (page != null)
{
return page.Redirect;
}
}
return section.DefaultRedirect;
}
static protected bool RunIt(HttpContext context){
CustomErrorsSection section = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
switch(section.Mode){
case CustomErrorsMode.Off:
return false;
case CustomErrorsMode.On:
return true;
case CustomErrorsMode.RemoteOnly:
return !(context.Request.UserHostAddress=="127.0.0.1");
default:
return true;
}
}
}
然后激活它,只需在global。asax
中添加一个小的东西 protected virtual void Application_Error (Object sender, EventArgs e)
{
CustomErrorsFixer.HandleErrors(Context);
}