ASP.NET MVC 2 和 IIS 7.0 的错误处理 [英] Error handling for ASP.NET MVC 2 and IIS 7.0

查看:22
本文介绍了ASP.NET MVC 2 和 IIS 7.0 的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我最近从 IIS 6.0 切换到 IIS 7.x,我正在寻找我梦想中的 ASP.NET MVC 2 错误处理技术.

I've recently switched from IIS 6.0 to IIS 7.x and I'm in search of error handling technique of my dream for ASP.NET MVC 2.

我想要达到的目标:

  • 在一处处理所有未处理的异常(最好在 Global.asax 处理程序中)

  • Handle all unhandled exceptions in one place (preferable in Global.asax handler)

404 和 403 错误的自定义处理程序(适用于 MVC 控制器操作和静态文件).这些处理程序不应执行重写,而应发送 HTTP 错误代码.例如,如果用户导航到 http://example.com/non-existing-page/ 他应该保留在此 URL 上,但会获得 HTTP 404 状态和自定义 404 页面.

Custom handlers for 404 and 403 errors (both for MVC controlleractions and static files). These handlers should not perform rewriting and should send HTTP error codes. For example if user navigates to http://example.com/non-existing-page/ he should remain on this URL, but get HTTP 404 status and custom 404 page.

能够通过操作以编程方式触发 404 和 403 错误.例如,如果用户在分页中指定了不存在的页码,如下所示:http://example.com/posts/page-99999/

Ability to trigger 404 and 403 errors programmatically from actions. For example if user specified non-existing page number in paging, like this: http://example.com/posts/page-99999/

如果这种错误处理对 VS 开发服务器也能正常工作,那就太好了(我知道 IIS Express,但现在我应该坚持使用 VS 开发服务器)

It will be great if this error handling will work the same for VS Development Server (I know about IIS Express, but for now I should stick to VS Dev Server)

我用过这个:http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx虽然在 IIS 6.0 上,但现在在带有集成管道的 IIS 7.0 上,我看到的是 IIS 错误消息而不是我的处理程序.

I've used this: http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx while being on IIS 6.0, but now on IIS 7.0 with integrated pipeline I see IIS error messages instead of my handlers.

提前致谢!

推荐答案

我用

protected void Application_Error(object sender, EventArgs e)

在我的 Global.asax.cs

要捕获所有未经处理的异常,在其中执行以下操作:

to catch all unhanded exceptions, doing something like this inside it:

try
{
    Response.Clear();
    var errorController = new ErrorController();
    var result = errorController.Error(statusCode, exception);
    result.ExecuteResult(new ControllerContext(new RequestContext(new HttpContextWrapper(Context), routeData), errorController));
    Server.ClearError();
}
catch(Exception e)
{
    HttpContext.Current.Response.StatusCode = 500;
    HttpContext.Current.Response.Write(e.Message);
}

我的错误控制器如下所示:

My Error controller looks like this:

public ActionResult Error(HttpStatusCode statusCode, Exception exception)
{
    var resource = new ErrorResource(statusCode, exception);
    this.response.StatusCode = resource.StatusCode;

#if !DEBUG
    return View("ReleaseError", resource);
#endif

    return View("DebugError", resource);            
}

我可以这样做:

throw new HttpException(404, "not found");

throw new HttpException(403, "not found);

以编程方式等.

我认为 MVC2 为错误情况引入了一个新的操作结果,但没有使用它,可能像框架的其余部分一样臭.

I think MVC2 introduced a new action result for error cases, not used it though, probably stinks like the rest of the framework.

这篇关于ASP.NET MVC 2 和 IIS 7.0 的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆