Restrict user to accesss NON route URL's - ASP.NET MVC

127 Views Asked by At

I have a webapplication with the following route:

routes.MapRoute(
        name: "Mytest",
        url: "Admin/Dashboard/Index",
        defaults: new { controller = "Dashboard", action = "Index", ID= "test" }
      );

Now when I go using http://localhost:13/Admin/Dashboard/Index it works, but http://localhost:13/Dashboard/Index also working.

But I would like, users can access http://localhost:13/Admin/Dashboard/Index ONLY. I need when users access http://localhost:13/Dashboard/Index should returns 404.

1

There are 1 best solutions below

0
Jackdaw On BEST ANSWER

To define route to be ignored use RouteCollectionExtensions.IgnoreRoute method:

public static void RegisterRoutes(RouteCollection routes)
{
    // IgnoreRoute() added by default 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.IgnoreRoute(url: "Dashboard/Index");

    routes.MapRoute(
        name: "Mytest",
        url: "Admin/Dashboard/Index",
        defaults: new { controller = "Dashboard", action = "Index", ID = "test" }
      );

    ... additional routes 

}