How to create custom error page for 401 in MVC
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