0

I am trying to route a link in MVC project.

I tried two methods:

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        //method1
       // routes.MapPageRoute("SchoolPage", "School", "~/home", false);

        //method2
        routes.MapRoute(name: "SchoolPage",url: "School", defaults: new { controller = "Home", action = "Index" });
    }

For both methods I get the error:

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

POIR
  • 3,110
  • 9
  • 32
  • 48

1 Answers1

1

You need to specify the custom route first. Default route has no constraints so it will match to any URL. When you make a request ot http://example.org/School, ASP.NET MVC will look for a controller named SchoolController using the default route. You have to make sure it matches to SchoolPage route first by placing it before the default route.

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

    routes.MapRoute(name: "SchoolPage",url: "School", defaults: new { controller =     "Home", action = "Index" });

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

}
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156