Search code examples
c#asp.net-mvcroutesrestrictionurl-shortener

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


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.


Solution

  • 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 
    
    }