簡體   English   中英

MVC自定義錯誤處理問題

[英]MVC Custom Error handling issue

我目前正在開發MVC應用程序。 我的問題是我無法讓我的customerror處理正常工作。

因此,本質上發生的是我拋出異常以在控制器中對此進行測試。 然后,應用程序應將用戶重定向到〜/ Shared / Errors.cshtml,但由於某種原因,它不會這樣做。

這是代碼。

拋出異常:

throw new Exception("Something went wrong");

Webconfig文件:

<customErrors mode="RemoteOnly"/>

ErrorController代碼:

  public class ErrorController : Controller
    {
        //
        // GET: /Error/
    public ActionResult Index()
    {
        return View();
    }

}

Error.cshtml中使用的代碼:

@model System.Web.Mvc.HandleErrorInfo
@{
    Layout = "_Layout.cshtml";
    ViewBag.Title = "Error";
}
<div class="list-header clearfix">
    <span>Error</span>
</div>
<div class="list-sfs-holder">
    <div class="alert alert-error">
        An unexpected error has occurred. Please contact the system administrator.
    </div>
    @if (Model != null && HttpContext.Current.IsDebuggingEnabled)
    {
        <div>
            <p>
                <b>Exception:</b> @Model.Exception.Message<br />
                <b>Controller:</b> @Model.ControllerName<br />
                <b>Action:</b> @Model.ActionName
            </p>
            <div style="overflow:scroll">
                <pre>
                    @Model.Exception.StackTrace
                </pre>
            </div>
        </div>
    }
</div>

預先感謝您提供的任何幫助

您應該返回這樣的視圖。

return View("~/Views/Shared/Errors.cshtml");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM