Can’t redirect error 500 to custom error page in ASP.NET MVC 5

I have been trying to learn how to create a custom error page for my ASP.NET MVC application. I have created a new controller with an action and a new view for this.

public ActionResult Error(int id, string message, string stackTrace)
{
    Response.StatusCode = id;
    ViewBag.Message = message;
    ViewBag.StackTrace = stackTrace;

    return View("~/Views/PaginaErro/PaginaErro.cshtml");
}

And this is the Application_Error method that I have added to the Global.asax.cs:

protected void Application_Error()
{
    Exception exception = Server.GetLastError();

    var httpException = exception as HttpException;

    int httpCode = httpException?.GetHttpCode() ?? (int)HttpStatusCode.InternalServerError;

    Server.ClearError();

    Response.Redirect("~/ErrorPage/Error?id=" + httpCode + "&message=" + HttpUtility.UrlEncode(exception.Message) + "&stackTrace=" + HttpUtility.UrlEncode(exception.StackTrace));
}

Web.config:

<customErrors mode="On" defaultRedirect="~/ErrorPage/Error">
    <error redirect="~/ErrorPage/Error/404" statusCode="404"/>
    <error redirect="~/ErrorPage/Error/500" statusCode="500"/>
</customErrors>

Now, when I receive an error 404, it works perfectly. I hits first the Application_Error method and then after that it goes to the Error action method in the ErrorPageController.

But the issue that I encounter is with the error 500. I can’t find what is causing that. Once it reaches the Response.Redirect, it never goes to my ErrorPageController, it keeps looping in the Application_Error.

The exception.Message of my http 500 error is:

The view ‘Error’ or its master was not found or no view engine supports the searched locations. The following locations were searched:rn~/Areas/Erp/Views/AjusteDeEstoque/Error.aspxrn~/Areas/Erp/Views/AjusteDeEstoque/Error.ascxrn~/Areas/Erp/Views/Shar…

I don’t have a view called Error, so I don’t know why is it searching for that one.

I’m not sure if this helps, but here is my RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

New contributor

Thiago Barbieri Catarina is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

First off, you should note that the info in web.config in your case is redundant – your Application_Error() function catches any exception and redirects it to the /ErrorPage/Error route.

I recreated your case and was having issues even entering Application_Error() with the web.config settings – I would encourage you to remove that section and then try again, unless you are adamant on keeping the web.config settings in which case you should change your approach.

Finally, your redirect will essencially be transmitting the error message and stack trace in the URL, which can potentially be too much information – you should rethink this way of passing information.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật