我看到一個很好的例子,其中的MVC控制器類從處理異常和內返回,像這樣與該錯誤的另外一個控制器類繼承:如何確保我的MVC應用程序在發生某種異常時重定向到錯誤頁面?
public class ApplicationController : Controller
{
public ActionResult NotFound()
{
return View("NotFound");
}
public ActionResult InsufficientPriveleges()
{
return View("InsufficientPriveleges");
}
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception is NotFoundException)
{
filterContext.Result = NotFound();
filterContext.ExceptionHandled = true;
return;
}
if (filterContext.Exception is InsufficientPrivelegesException)
{
filterContext.Result = InsufficientPriveleges();
filterContext.ExceptionHandled = true;
return;
}
base.OnException(filterContext);
}
}
不過,我已經注意到,例如說我的控制器正在部分視圖中加載用戶不應訪問的內容,錯誤將顯示在頁面上的部分視圖中。
我希望整個頁面顯示錯誤,實際上,異常應該重定向到一個全新的頁面。我如何使用上面顯示的當前課程來實現這一目標?
我認爲這會回答你的問題:http://stackoverflow.com/questions/550995/get-filter-redirect-to-action –
我似乎不能扔孩子的行動中一個例外。我認爲我需要能夠在服務層的任何位置拋出一個異常,而不管我在渲染管道中的哪個位置,並讓其重新進行。有任何想法嗎? – jaffa
您介意顯示有問題的局部視圖和相關操作嗎?也許它處理正確的情況下部分視圖... –