Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to change the default controller in ASP.NET MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.66k
    Comment on it

    Many times we need to change the default controller in ASP.NET MVC. To change this first we need to understand how the default route is configured. This setting is available in RouteConfig file (under App_Start folder).

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

    The above setting means that when we run the application , Home controller is called and it's Index method is executed. If we want to set a new default route for the application we simply have to change the above setting. Let us say we want to start the application by loading Catalogue inside Inventory controller. We just need to make changes in the settings as below

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

    After making the above changes , the application will load Catalogue at start time.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: