Custom error page in mvc

I need to display the error page using customerror on web.config

But no matter what the error may be, even in web.config I pointed out that some version is incorrect, you also need to show the error page,

How can i do this. I tried but redirected the urls to

"http://localhost:1966/Error.html?aspxerrorpath=Error.html"

Tag CustomError:

<customErrors mode="On" defaultRedirect="Error.html" />

And another error page from mvc appears, not mines.

+3
source share
2 answers

In ASP.NET MVC, error handling is usually specified using the HandleError attribute . By default, the View named Error is used to display the custom error page. If you just want to customize this view, you can edit Views / Shared / Error.aspx.

, View.

:

[HandleError(View = "CustomError")]
public ViewResult Foo() 
{
    // ...
}

ASP.NET MVC . .

+7

Mark, . HandleError attrib.

, , . OnException baseclass ovveride, , , , "~/Shared/Error.aspx"

<customErrors mode="On" >, web.config, .

public class BaseController : Controller
{
        ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public BaseController()
    {
    }

    protected override void OnException(ExceptionContext filterContext)
    {
        // Log the error that occurred.
        log.Fatal("Generic Error occured",filterContext.Exception);

        // Output a nice error page
        if (filterContext.HttpContext.IsCustomErrorEnabled)
        {
            filterContext.ExceptionHandled = true;
            View("Error").ExecuteResult(ControllerContext);
        }
    }

}

" ".

, 404, mapRoute global.asax RegisterRoutes ( RouteCollection)

// Show a 404 error page for anything else.
            routes.MapRoute(
                "Error",
                "{*url}",
                new { controller = "Shared", action = "Error" }
            );
+1

Source: https://habr.com/ru/post/1727862/


All Articles