2

ほとんどの場合正常に機能するレイアウトのエラーページがありますが、部分ビューを返すコントローラーでエラーが発生すると、エラーページとそのレイアウトが部分ビューに配置されます。それは論理的だと思いますが、エラーページをフルページとしてロードしたいと思います。すべてのエラー処理を変更せずにそれを達成するにはどうすればよいですか。

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>
4

1 に答える 1

2

try catchブロックを使用するだけで、catchでPartialView()の代わりにView()を返すことができます。

Function GetData() As ActionResult
Try
...
Return PartialView("_Tab", vmData)
Catch ex as Exception
//Handle exception here ( send to error log, etc)
Return View("~/SystemPages/ErrorPage")
End Try

また

web.config:

<customErrors mode="On"/>

BaseController:

Protected Overrides Sub OnException(ByVal filterContext As ExceptionContext)

    If filterContext Is Nothing Then Return

    Dim Message As String

    If TypeOf (filterContext.Exception) Is FaultException Then
        Dim CodeName As String = 
        CType(filterContext.Exception, FaultException).Code.Name
        Message = CType(filterContext.Exception, FaultException).Message
    Else
        Logging.LogDebugData(HamtaDebugInformation(filterContext.RouteData))
        Logging.WriteExceptionLog(filterContext.Exception)
        Message = filterContext.Exception.Message
    End If

    Response.Redirect(String.Format("~/Error/HttpError/?message={1}", "HttpError", Message))

End Sub

ErrorController:

public class ErrorController : Controller
{
    // GET: /Error/HttpError
    public ActionResult HttpError(string message) {
       return View("ErrorTest", message);
}

この投稿:ASP.NETMVCカスタムエラー処理Application_ErrorGlobal.asax?

各タイプのエラーを個別に処理する方法について説明します。global.asaxファイルではなくbasecontrollerで例外を処理していることに注意してください。例外処理を変更できた場合は、それがより良い方法です。

于 2012-10-26T19:52:57.213 に答える