C# MVC 모드 404 500 페이지 설정 방법
8560 단어 mvc
<customErrors mode="On" defaultRedirect="Controllers/Action">
<error statusCode="403" redirect="Controllers/Action" />
<error statusCode="404" redirect="Controllers/Action" />
</customErrors>
Controllers Action
방법 2:
protected void Application_EndRequest()
{
var statusCode = Context.Response.StatusCode;
var routingData = Context.Request.RequestContext.RouteData;
if (statusCode == 404 || statusCode == 500)
{
Response.Clear();
var area = DataHelper.ConvertTo(routingData.DataTokens["area"], string.Empty);
if (area == "Admin")
{
Response.RedirectToRoute("Admin_Default", new { controller = "BackError", action = "NotFound", IsReload = 1 });
}
else
{
Response.RedirectToRoute("Default", new { controller = "Error", action = "NotFound", id = UrlParameter.Optional });
}
}
}
방법 3:
Global.aspx.cs
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new CustomHandlerErrorAttribute());
}
CustomHandlerErrorAttribute.cs
public class CustomHandlerErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
{
return;
}
filterContext.Controller.ViewData.Model = filterContext.Exception;
filterContext.Result = new ViewResult
{
ViewName = "Error",
ViewData = filterContext.Controller.ViewData
};
filterContext.ExceptionHandled = true;
}
}
web.config
<customErrors mode="On">
<error redirect="/home/error" statusCode="404" />
</customErrors>
web.config
<httpErrors errorMode="Custom" existingResponse="PassThrough">
</httpErrors>
Error.cshtml
<div class="box">
@{
var exception = ViewData.Model;
var statusCode = exception == null ? 404 : 500;
Response.StatusCode = statusCode;
if (statusCode == 404)
{
<h3>404 Page not found!</h3>
<p> !</p>
}
else if (statusCode == 500)
{
<h3>500 </h3>
<p>@exception.Message</p>
}
}
<p style="font-size: 12px; color: Gray"> !</p>
</div>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
클린 아키텍처의 Presenter를 이해하기 어려운 것은 MVC 2가 아니기 때문에클린 아키텍처에는 구체적인 클래스 구성 예를 보여주는 다음 그림이 있습니다. 이 그림 중에서 Presenter와 Output Boundary(Presenter의 인터페이스)만 구체 구현을 이미지하는 것이 매우 어렵다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.