2
我有一個錯誤頁面,其佈局在大多數情況下都能正常工作,但當控制器返回部分視圖時出現錯誤時,錯誤頁面及其佈局將放置在局部視圖中。我想這就是邏輯,但我想錯誤頁面被加載爲整頁。我如何在不更改所有錯誤處理的情況下完成該操作在部分視圖中加載MVC錯誤頁面
的web.config:
<customErrors mode="On" defaultRedirect="~/Error">
<error statusCode="500" redirect="~/SystemPages/ErrorPage" />
<error statusCode="403" redirect="~/SystemPages/FileNotFound" />
<error statusCode="404" redirect="~/SystemPages/FileNotFound" />
</customErrors>
Global.asax中:
Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
filters.Add(New HandleErrorAttribute())
End Sub
BaseController:
Protected Overrides Sub OnException(ByVal filterContext As ExceptionContext)
If filterContext Is Nothing Then Return
If TypeOf (filterContext.Exception) Is FaultException Then
Dim CodeName As String =
CType(filterContext.Exception, FaultException).Code.Name
Dim Message As String = CType(filterContext.Exception, FaultException).Message
TempData("ErrorMessage") = Message
Else
Logging.LogDebugData(HamtaDebugInformation(filterContext.RouteData))
Logging.WriteExceptionLog(filterContext.Exception)
TempData("ErrorMessage") = filterContext.Exception.Message
End If
Response.Redirect("/SystemPages/ErrorPage")
End Sub
SearchController:
Function GetData() As ActionResult
...
Return PartialView("_Tab", vmData)
的errorPage:
@Code
ViewData("Title") = "ErrorPage"
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
<div id="mainContent" class="oneColumn">
<div class="panel">
<span class="panelTLC"></span>
<span class="panelTRC"></span>
<div id="inputPanel" class="panelContent">
<div class="modul">
<div class="modulHead">
<span class="TLC"></span>
<span class="TRC"></span>
</div>
<div class="modulContent">
<span class="TLC"></span><span class="TRC"></span>
<p>@ViewBag.ErrorMessage</p>
<p>@TempData("ErrorMessage")</p>
<span class="BLC"></span>
<span class="BRC"></span>
</div>
</div>
</div>
<span class="panelBLC"></span><span class="panelBRC"></span>
</div>
</div>
謝謝@ rbj325!我喜歡第一種方法,但ErrorPage不是視圖,因此不起作用。如果我返回一個像這樣的錯誤視圖instad,返回視圖(「ErrorTest」),它仍然會被加載到局部視圖中並弄亂佈局。我對MVC很陌生,所以我不太瞭解第二種方法。 – user1777259
再次感謝!但錯誤頁面仍然在部分視圖中加載。我發現唯一的辦法是在catch塊中有這個javascript重定向,但我認爲這是一個難以解決問題的方法。返回JavaScript(String.Format(「document.location ='{0}'」,「/ SystemPages/ErrorPage」)) – user1777259
你能發佈你的錯誤頁面代碼嗎?您可以將任何頁面轉換爲視圖。您可能需要覆蓋佈局頁面。 – rbj325