0

How to remove the controller name from the URL in MVC 5. I tried to add the route in the route.config

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
           name: "Special",
           url: "action/{id}",
           defaults: new { controller = "Home", action = "LandingIndex", id = UrlParameter.Optional }
       );
    }
}

When I tried to a access the URL by http://localhost:24220/LandingIndex it will prompt 404 error. how to resolve this issue

tereško
  • 58,060
  • 25
  • 98
  • 150
Kashif Saeed
  • 127
  • 2
  • 12
  • This is your answer : https://stackoverflow.com/questions/57799273/how-to-remove-controller-name-from-url-in-mvc-project/57799331#57799331 – Pritesh Sep 07 '19 at 04:51

2 Answers2

1

You could try inverting the routes definition by placing the more specialized route first. Also you probably didn't want to hardcode the action name as action but rather use the {action} placeholder:

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

        routes.MapRoute(
            name: "Special",
            url: "{action}",
            defaults: new { controller = "Home", action = "LandingIndex" }
        );

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

Now when you navigate to /LandingIndex, the LanginIndex action on the Home controller should be invoked.

Also notice that I have removed the optional {id} placeholder from the first route. The reason for that is because if it were present, the routing engine would never use the second route. For example if you try to load /Home/Index it will look for a Home action on the Home controller and passing it Index as id. If you want to have this {id} token you will need to use a constraint to diambiguate with your second more general route definition. For example if your identifiers are always integers or follow some pattern, you could write a regex constraint that will match them.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • on the controller side can we access it by adding the attribute of route on top of that controller – Kashif Saeed Jan 07 '16 at 10:41
  • You choose between attribute based routing or explicit routing definition in your Global.asax. Do not mix the two. – Darin Dimitrov Jan 07 '16 at 10:42
  • Yes it resolved my confusion thanks all work well only remove the following line from the above responce code will make it runable as expected. `routes.MapMvcAttributeRoutes();` – Kashif Saeed Jan 07 '16 at 11:06
1

Thank you Darin Dimitrov little change in your replied code helps me to achieve the expected results

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

    routes.MapRoute(
        name: "Special",
        url: "{action}",
        defaults: new { controller = "Home", action = "LandingIndex" }
    );

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

just to remove the following line helps me

routes.MapMvcAttributeRoutes();

Kashif Saeed
  • 127
  • 2
  • 12