[英]How to display custom error messages of IIS Server in ASP.net MVC - 5
I am new to web development and I need to display custom error message that comes from IIS Server in MVC-5 application. 我是Web开发的新手,我需要在MVC-5应用程序中显示来自IIS服务器的自定义错误消息。 for example "504 gateway time out error" or "500 Error".
例如“ 504网关超时错误”或“ 500错误”。 I am working on already existing code.
我正在研究现有的代码。 Below is what I have till now.
以下是我到目前为止所拥有的。
BaseController: BaseController:
protected override void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled)
{
filterContext.ExceptionHandled = true;
Logger.Error("SimController", "OnException", "An exception has occurred.", filterContext.Exception);
// this will cause the error to be displayed to the user..
Response.StatusCode = 500;
Response.StatusDescription = Constants.DISPLAYED_ERROR_MESSAGE_SHARED;
if (filterContext.Exception.GetType() == typeof(ValidationException))
{ Response.StatusDescription += Constants.ADDITIONAL_DETAIL_ERROR_MESSAGE + filterContext.Exception.Message; }
else
{ Response.StatusDescription += Constants.GENERIC_ERROR_MESSAGE; }
}
}
ErrorController: ErrorController:
public ActionResult Index(string Message)
{
// We choose to use the ViewBag to communicate the error message to the view
ViewBag.Message = Message;
return View();
}
public ActionResult Oops(string Message)
{
// We choose to use the ViewBag to communicate the error message to the view
ViewBag.Message = Message;
return View();
}
Web.Config Web.Config中
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5" />
<customErrors mode="On" defaultRedirect="~/ErrorPage/Oops">
<error redirect="~/Error/Oops/404" statusCode="404" />
<error redirect="~/Error/Oops/500" statusCode="500" />
</customErrors>
</system.web>
Global.asax Global.asax
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute());
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
}
I am new to coding so I have put whatever I could in this post. 我是编码的新手,所以我在这篇文章中投入了一切。 So, is that it?
那是吗? I am still not able to see the custom page for 500.
我仍然看不到500的自定义页面。
The way I am simulating the error is 我模拟错误的方式是
HomeController: HomeController的:
public ActionResult Index()
{
return new HttpStatusCodeResult(500);
return View();
}
Post I referrred : Custom Error 我推荐的帖子: 自定义错误
Oops(string Message)
takes message as an argument. Oops(string Message)
将message作为参数。 You pass there error code: redirect="~/Error/Oops/404"
. redirect="~/Error/Oops/404"
。 <customErrors mode="On">
. <customErrors mode="On">
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.