0

Please I need to be guided to set a default action for my controllers such that anytime there is no action indicated in a controller, the application will route to the index action on the given controller. For example, if someone navigates to dashboard/ without indicating the action, the application automatically runs the index action of the dashboard controller.

The following shows my attempts but it is still not working.

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

I will appreciate any guide to get it working right,

3
  • 1
    First of all, MVC Routing examines the routes in the order they are added. The first two routes in your code have exactly the same configuration, so the Regular route will never be reached, nor the third one that is contained in the above ones. I suggest that if you want to redirect to a login page when somebody enter to your site, do it from an authentication filter, and do not use the routing as it is not its purpose. Commented Apr 28, 2016 at 19:05
  • My problem is not setting a default controller. Its rather setting a default method for every controller. I want to make Index method to be called in any controller if no method is indicated. That's what I am looking for Commented Apr 28, 2016 at 19:23
  • 1
    Then, use the default route that MVC template provides. Michael Berezin has posted it in his answer. Commented Apr 28, 2016 at 20:21

1 Answer 1

1

you have to different route on the same url. you only need to use the default route.

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

you can change "Home" to "Dashboard" if that is your default controller.

Sign up to request clarification or add additional context in comments.

2 Comments

My problem is not setting a default controller. Its rather setting a default method for every controller. I want to make Index method to be called in any controller if no method is indicated.
that route does that.the definition of the route is: if {controller} is empty use "Home" if {action} is empty use "index" if {id} has value place in the id parameter. for every url that has only the controller name, the request should go the index action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.