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

查看:143
本文介绍了错误处理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.

我想才达到什么:


  • 处理在一个地方所有未处理的异常(preferable在Global.asax中处理)

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

有关404和403错误自定义处理程序(无论是MVC控制器\\动作和静态文件)。这些处理器不应执行重写,并应发送HTTP错误codeS。
例如,如果用户导航到 http://example.com/non-existing-page/ 他应该留在这个URL,但得到HTTP 404状态和定制的404页。

Custom handlers for 404 and 403 errors (both for MVC controller\actions 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/文章/页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防爆preSS,但现在我要坚持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)

我用这个:<一href=\"http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx\" rel=\"nofollow\">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

要捕获所有异常unhanded,做这样的事情里面:

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天全站免登陆