2014-09-15 2 views
2

ASP.NET MVC 2 응용 프로그램에서 사용자 정의 403 페이지를 표시하려고합니다. 나는 link을 따라 갔다. 구성 파일에 다음을 추가했습니다.ASP.NET MVC 2의 사용자 정의 403 오류 페이지

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

여전히 기본 ASP.NET 403 오류 페이지가 표시됩니다. 뭐가 문제 야? the default ASP.NET 403 error page

+0

시도해주세요. http://stackoverflow.com/questions/25844627/custom-error-page-when-http-error-occured-without-changing-url/25844739#25844739 –

+0

@HirenKagrana Tried. 나를 위해 일하지 않습니다. 500을 받고 응용 프로그램이 시작되지 않습니다. 또한 리디렉션과 같은 특정 요구 사항이 없습니다. 난 그냥 사용자 정의 오류 페이지를 원한다. – Maxsteel

답변

4

Web.config의 마크 업 아래에있는 추가 :

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); 
     } 
    } 
} 
:

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 만들기 :

<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라는 이름의 뷰 모델을 추가

아래의 마크 업과 함께 \ 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> 
+0

나는 이것을 시도하고있다. 그러나'ErrorInfo'는 유효한 타입이 아닙니까? 나는 어떤 refernce가 없는가? – Maxsteel

+0

방금 ​​ErrorInfo 클래스를 사용하여 답변을 업데이트했습니다. 확인해주십시오. – abdulbasit

1

기본적으로 MVC 템플릿은 HandleErrorAttribute att를 구현합니다. 우리는 CustomErrors가가의 web.config에 설정되어있는 경우

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

HandleErrorAttribute 기본 오류 페이지로 사용자를 리디렉션 Global.asax에이를 찾아 (또는 App_Start \ FilterConfig.cs에서 MVC4의 경우) 할 수 있습니다.

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

구문 : 아래 그림과 같이

이 HandleErrorAttribute 필터에 의한 사용자 지정 오류 처리를 사용하려면, 우리는 응용 프로그램의 web.config의 system.web 섹션에 customErrors에 요소를 추가 할 필요가

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

다음과 같이 별도의보기를 사용하여 사용자를 오류 상태 코드를 기반으로 특정보기로 리디렉션 할 수 있습니다.

이제 HandleErrorAttribute가 어떻게 사용자를 기본 Error.cshtml보기로 리디렉션하는지 확인할 수 있습니다. 아래와 같이 테스트하려면, 로그인 컨트롤러의 색인 작업에서 예외가 발생 할 수 있습니다 : 우리는 올바른 500을 반환하는 기본 MVC 프로젝트의 공유 폴더에 Errors.cshtml에서 기본 출력이 표시됩니다

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

상태 메시지가 있지만 스택 추적은 어디에 있습니까 ?? 이제 아래와 같이

, 우리는 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 [email protected] or call us at (xxx) xxx-xxxx.</h5> 
         </div> 
        } 

       } 
    </div> 
    </hgroup> 

이 변화는 우리가 자세한 스택 추적을 볼 수 있는지 확인합니다.

이 방법을 시도해보십시오.

0

많은 좋은 제안이 있습니다. 내 스튜디오에서는 Visual Studio에서 Visual Studio가 발생했고 Visual Studio는 다른 웹 프로젝트를 동일한 포트로 시작했는데 웹 프로젝트가 config로로드되도록 지정 되었기 때문에 다른 projet에서 오류가 새로 고쳐졌습니다 (a global.asax) 두 번째 웹 프로젝트에 어떤 변화를 주 었는지에 관계없이.

근본적인 문제는 내 다른 사이트가 Visual Studio에서 동일한 포트를 구성했기 때문에 솔루션 프로젝트 순서에 따라로드를 계속 유지하고 포트를 사용하는 것으로 나타났습니다. 나는 그것을 다른 항구로 바꾸었고 문제는 사라졌다.