簡體   English   中英

ASP.NET MVC 2中的自定義403錯誤頁面

[英]Custom 403 error page in ASP.NET MVC 2

我想在我的ASP.NET MVC 2應用程序中顯示自定義403頁面。 我按照以下鏈接 我在配置文件中添加了以下內容:

<httpErrors>
      <remove statusCode="403" subStatusCode="-1"/>
      <error statusCode="403" path="/403.htm" responseMode="ExecuteURL"/>
</httpErrors>

我仍然看到默認的ASP.NET 403錯誤頁面。 怎么了? 默認的ASP.NET 403錯誤頁面

在web.config中添加以下標記:

 <customErrors mode="On" defaultRedirect="/error/error">
  <error statusCode="400" redirect="/error/badrequest" />
  <error statusCode="403" redirect="/error/forbidden" />
  <error statusCode="404" redirect="/error/notfound" />
  <error statusCode="414" redirect="/error/urltoolong" />
  <error statusCode="503" redirect="/error/serviceunavailable" />
</customErrors>

使用以下代碼添加名為ErrorInfo的視圖模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Gunaatita.ViewModel
{
    public class ErrorInfo
    {
        public string Message { get; set; }
        public string Description { get; set; }
    }
}

使用以下代碼創建一個控制器名稱ErrorController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gunaatita.ViewModel;

namespace Gunaatita.Controllers
{
    [HandleError]
    public class ErrorController : Controller
    {
        public ActionResult Error()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "An Error Has Occured";
            errorInfo.Description = "An unexpected error occured on our website. The website administrator has been notified.";
            return PartialView(errorInfo);
        }
        public ActionResult BadRequest()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "Bad Request";
            errorInfo.Description = "The request cannot be fulfilled due to bad syntax.";
            return PartialView("Error", errorInfo);
        }
        public ActionResult NotFound()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "We are sorry, the page you requested cannot be found.";
            errorInfo.Description = "The URL may be misspelled or the page you're looking for is no longer available.";
            return PartialView("Error", errorInfo);
        }

        public ActionResult Forbidden()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "403 Forbidden";
            errorInfo.Description = "Forbidden: You don't have permission to access [directory] on this server.";
            return PartialView("Error", errorInfo);
        }
        public ActionResult URLTooLong()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "URL Too Long";
            errorInfo.Description = "The requested URL is too large to process. That’s all we know.";
            return PartialView("Error", errorInfo);
        }
        public ActionResult ServiceUnavailable()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "Service Unavailable";
            errorInfo.Description = "Our apologies for the temporary inconvenience. This is due to overloading or maintenance of the server.";
            return PartialView("Error", errorInfo);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}

並使用以下標記更新\\ Views \\ Shared \\ Error.cshtml:

@model Gunaatita.ViewModel.ErrorInfo
@{
    ViewBag.Title = "Problem";
    Layout = "~/Views/Shared/_LayoutSite.cshtml";
}

<div class="middle-container">


    <link rel="stylesheet" href="/Content/css/thankyou.css">
    <!--- middle Container ---->

    <div class="middle-container">
        <div class="paddings thankyou-section" data-moduleid="2050" id="ContactUsPane">
            @if (Model != null)
            {
                <h1>@Model.Message</h1>
                <p>@Model.Description</p>
            }
            else
            {
                <h1>An Error Has Occured</h1>
                <p>An unexpected error occured on our website. The website administrator has been notified.</p>
            }

            <p><a href="/" class="btn-read-more">Go To Home Page</a></p>
        </div>
    </div>
    <!--- middle Container ---->

</div>

默認情況下,MVC模板實現HandleErrorAttribute att。 我們可以在Global.asax中找到它(或者在App_Start \\ FilterConfig.cs中找到MVC4)

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)   {
    filters.Add(new HandleErrorAttribute());  
   }

如果在web.config中打開CustomErrors,HandleErrorAttribute會將用戶重定向到默認的錯誤頁面。

要通過HandleErrorAttribute過濾器啟用自定義錯誤處理,我們需要在應用程序的Web.config的system.web部分中添加customErrors元素,如下所示:

<system.web>
  <customErrors mode="On" defaultRedirect="Error.cshtml" />
</system.web>

句法 :

<customErrors defaultRedirect="url"   mode="On | Off | RemoteOnly"> 
</customErrors>

我們也可以有單獨的視圖,根據錯誤狀態代碼將用戶重定向到特定視圖,如下所示:

<customErrors mode="On">
    <error code="404" path="~/Views/Shared/NotFound.cshtml" /> 
    <error code="500" path="~/Views/Shared/InternalServerError.cshtml" /> 
</customErrors> 

現在讓我們看看HandleErrorAttribute如何將用戶重定向到默認的Error.cshtml視圖。 要對此進行測試,請從Login Controller的Index操作中拋出異常,如下所示:

public ActionResult Index()  {  
    throw new ApplicationException("Error");  
    //return View();  
}

我們將在默認MVC項目的Shared文件夾中看到Errors.cshtml的默認輸出,它將返回正確的500狀態消息但是我們的堆棧跟蹤在哪里?

現在要捕獲Stack Trace,我們需要在Error.cshtml中做一些修改,如下所示:

 @model System.Web.Mvc.HandleErrorInfo
    <hgroup>
      <div class="container">
                <h1 class="row btn-danger">Error.</h1>
    @{
                    if (Request.IsLocal)
                    {
                        if (@Model != null && @Model.Exception != null)
                        {
                            <div class="well row">

                                <h4> Controller: @Model.ControllerName</h4>
                                <h4> Action: @Model.ActionName</h4>
                                <h4> Exception: @Model.Exception.Message</h4>
                                <h5>
                                    Stack Trace: @Model.Exception.StackTrace
                                </h5>
                            </div>

                        }
                        else
                        {
                            <h4>Exception is Null</h4>
                        }
                    }
                    else
                    {
                        <div class="well row">
                            <h4>An unexpected error occurred on our website. The website administrator has been notified.</h4>
                            <br />
                            <h5>For any further help please visit <a href="http://abcd.com/"> here</a>. You can also email us anytime at support@abcd.com or call us at (xxx) xxx-xxxx.</h5>
                        </div>
                    }

                }
    </div>
    </hgroup>

此更改可確保我們看到詳細的堆棧跟蹤。

嘗試這種方法,看看。

這里有很多好的建議; 對我來說,它發生在視覺工作室和視覺工作室開始一個不同的web項目進入同一個端口,因為我的web項目被指定加載它的配置,所以它只是刷新我的其他項目的錯誤(沒有一個global.asax)無論我對第二個Web項目做了什么改變。

根本問題是我的其他網站在visual studio中配置了相同的端口,因此由於解決方案項目訂單和耗盡端口而導致其首先加載。 我把它改成了另一個端口,問題就消失了。

暫無
暫無

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

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