ASP.NET MVC HandleError我如何[HandleError]在asp.net MVC Preview 5中進行過濾?我在我的Web.config文件中設置了customErrors<customErrors mode="On" defaultRedirect="Error.aspx">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/></customErrors>并將[HandleError]放在我的Controller類上面,如下所示:[HandleError]public class DSWebsiteController: Controller{
[snip]
public ActionResult CrashTest()
{
throw new Exception("Oh Noes!");
}}然后我讓我的控制器從這個類繼承并在它們上調(diào)用CrashTest()。視覺工作室在錯誤時停止,按f5繼續(xù)后,我被重新路由到Error.aspx?aspxerrorpath = / sxi.mvc / CrashTest(其中sxi是使用過的控制器的名稱。當然道路無法找到,我得到“'''應用程序中的服務器錯誤?!?04。這個站點從預覽3移植到5.除了錯誤處理之外,所有東西都運行(沒有太多工作要移植)。當我創(chuàng)建一個完整的新項目時,錯誤處理似乎有效。想法?- 注意 -由于這個問題現(xiàn)在有超過3K的視圖,我認為放入我目前使用的(ASP.NET MVC 1.0)是有益的。在mvc contrib項目中有一個名為“RescueAttribute”的出色屬性你也應該檢查一下;)
3 回答

牧羊人nacy
TA貢獻1862條經(jīng)驗 獲得超7個贊
[HandleError]
當你只為你的類提供HandleError屬性時(或者你的動作方法),當發(fā)生未處理的異常時,MVC將首先在Controller的View文件夾中查找名為“Error”的相應視圖。如果它找不到它,那么它將繼續(xù)查看共享視圖文件夾(默認情況下應該包含一個Error.aspx文件)
[HandleError(ExceptionType = typeof(SqlException), View = "DatabaseError")][HandleError(ExceptionType = typeof(NullReferenceException), View = "LameErrorHandling")]
您還可以使用有關(guān)所查找的異常類型的特定信息來堆疊其他屬性。此時,您可以將錯誤定向到默認“錯誤”視圖以外的特定視圖。
有關(guān)更多信息,請查看Scott Guthrie關(guān)于它的博客文章。

紅顏莎娜
TA貢獻1842條經(jīng)驗 獲得超13個贊
[HandleError] public class ErrorController : Controller { [AcceptVerbs(HttpVerbs.Get)] public ViewResult NotAuthorized() { //401 Response.StatusCode = (int)HttpStatusCode.Unauthorized; return View(); } [AcceptVerbs(HttpVerbs.Get)] public ViewResult Forbidden() { //403 Response.StatusCode = (int)HttpStatusCode.Forbidden; return View(); } [AcceptVerbs(HttpVerbs.Get)] public ViewResult NotFound() { //404 Response.StatusCode = (int)HttpStatusCode.NotFound; return View(); } public ViewResult ServerError() { //500 Response.StatusCode = (int)HttpStatusCode.NotFound; return View(); }
}
- 3 回答
- 0 關(guān)注
- 461 瀏覽
添加回答
舉報
0/150
提交
取消