asp.net-mvc - MVC错误页面在部分 View 中加载

标签 asp.net-mvc error-handling partial-views

我有一个错误页面,其布局在大多数情况下都可以正常工作,但是当返回部分 View 的 Controller 中出现错误时,错误页面及其布局将放置在部分 View 中。我想这是合乎逻辑的,但我希望错误页面作为整页加载。如何在不更改所有错误处理的情况下实现这一目标。

网络配置:

<customErrors mode="On" defaultRedirect="~/Error">
  <error statusCode="500" redirect="~/SystemPages/ErrorPage" />
  <error statusCode="403" redirect="~/SystemPages/FileNotFound" />
  <error statusCode="404" redirect="~/SystemPages/FileNotFound" />
</customErrors>

全局.asax:

Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
    filters.Add(New HandleErrorAttribute())
End Sub

基础 Controller :

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

搜索 Controller :

   Function GetData() As ActionResult
   ...
   Return PartialView("_Tab", vmData)

错误页面:

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

最佳答案

您可以只使用 try catch block ,并在 catch 中返回 View() 而不是 PartialView()。

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

或者

网络配置:

<customErrors mode="On"/>

基础 Controller :

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

错误 Controller :

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

这篇文章:ASP.NET MVC Custom Error Handling Application_Error Global.asax?

介绍如何分别处理每种类型的错误。请记住,您是在 basecontroller 而不是 global.asax 文件中处理异常。如果您能够更改异常处理,那将是更好的方法。

关于asp.net-mvc - MVC错误页面在部分 View 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13088593/

相关文章:

com - ISupportErrorInfo 的实现 - 这意味着什么?

c# - 无法在 JQuery 中获取/绑定(bind)正确的实体 ID,始终从迭代中获取第一个项目 ID

c# - EF4 Code First 数据库生成

php - 创建基本的用户登录名。 $ _SESSION抛出错误

random - 在systemverilog中使用随机随机1位和2位错误

ruby-on-rails - 局部变量没有通过渲染传递给部分模板?

ruby-on-rails - 仅在列为真时渲染部分项目

c# - 当 View mvc 中的表单中有 foreach 循环时,action 方法的参数应该是什么

javascript - 将 JavaScript 值分配给 mvc 模型属性

c# - 获取 IEnumerable 错误 : CS1061 does not contain C# ASP. NET MVC5