I have an MVC app which has the following route config
In Global.ascx
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
In the RouteConfig.cs I have
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Now if i type in the browser , https://localhost/users this will take me to the
UsersController and call the Index() ActionResult. In there i do a check to see if the
user has access to the view or not as follows:
public ActionResult Index()
{
if (<User has access condition check>)
{
return View();
}
return View("~/Views/PermissionError.cshtml");
}
The issue is that I have about 30 pages in my app that the user can browse to by typing in the broswer url. So instead of doing the check in every Index ActionResult , is there a way i can do the check in my route config or somewhere else that does the permission check and if they are allowed to view the page it will continue to the page else it will show the error page ?