Rohit Singh

Rohit Singh

  • NA
  • 749
  • 84.7k

How to create custom error page for 401 in MVC

Aug 16 2017 4:09 AM
Hi. I've been facing this issue so long and tried almost everything but couldn't get what I want.

I want to display an custom error page for code 401 , if the user is not authorized to view any particular part of my webApplication. Kindly solve my problem.

What I have tried:

This is my web.config file:

<customErrors mode="On">
<error statusCode="404" redirect="~/Error/NotFound"/>
<error statusCode="401" redirect="~/Home/SecureMethod"/>



This is My Controller:

[HandleError]
public class HomeController : Controller
{

[Authorize]
public ActionResult SecureMethod()
{


return View();
}

[AllowAnonymous]
public ActionResult NonSecureMethod()
{
return View();
}

I have also enabled the Windows Authentication and Anonymous Authentication on IIS Server

Answers (2)