How to set Default Controller in asp.net MVC 4 & MVC 5

170,772

Solution 1

the best way is to change your route. The default route (defined in your App_Start) sets /Home/Index

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }
);

as the default landing page. You can change that to be any route you wish.

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "Sales", action = "ProjectionReport", 
        id = UrlParameter.Optional }
);

Solution 2

Set below code in RouteConfig.cs in App_Start folder

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 });
}

IF still not working then do below steps

Second Way : You simple follow below steps,

1) Right click on your Project

2) Select Properties

3) Select Web option and then Select Specific Page (Controller/View) and then set your login page

Here, Account is my controller and Login is my action method (saved in Account Controller)

Please take a look attachedenter image description here screenshot.

Solution 3

I didnt see this question answered:

How should I setup a default Area when the application starts?

So, here is how you can set up a default Area:

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

Solution 4

In case you have only one controller and you want to access every action on root you can skip controller name like this

routes.MapRoute(
        "Default", 
        "{action}/{id}", 
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }
);
Share:
170,772

Related videos on Youtube

Adrian10 BEN
Author by

Adrian10 BEN

Web Developer passionate about .Net frameworks.

Updated on February 26, 2020

Comments

  • Adrian10 BEN
    Adrian10 BEN about 4 years

    How do I set Default Controller for my ASP.NET MVC 4 project without making it HomeController?

    How should I setup a default Area when the application starts?

    • Andre Mesquita
      Andre Mesquita about 9 years
      as would be changing from "/" to "/home as root page? This "/home" must be visible at customer's browser.
  • Luis D Urraca
    Luis D Urraca about 11 years
    In MVC 4 the default route is set in the App_Start/RouteConfig.cs
  • Dave Alperovich
    Dave Alperovich about 11 years
    Very true. I'm currenlty working on projects in both and overlook these things ;)
  • Good Samaritan
    Good Samaritan almost 11 years
    Dave is right, but I would like to add that in MVC 4 the routing is no longer done in Global.asax, but rather in the RouteConfig.cs under App_Start.
  • superjos
    superjos over 10 years
    How to make that work when the controller referenced in default route sits in another area (i.e. not the root one)?
  • Dave Alperovich
    Dave Alperovich over 10 years
    you should be able to make the default route go to "AreaName/{controller}/{action}/{id}"
  • Stuart Dobson
    Stuart Dobson about 10 years
    great question, why don't you create it as one?
  • Martin Liversage
    Martin Liversage over 9 years
    Your "second way" is about what happens when you debug your web project. It has nothing to do with the "default controller", i.e. what page is shown when you navigate to the landing page of your site.
  • Gus Crawford
    Gus Crawford over 9 years
    What is an 'Area'? If I define a View, is the referenced layout loaded, and then a separate HTTP request with my view data? (i.e. separate Ajax operation) or is the layout rendered and wrapped around my view?
  • Amna Ali
    Amna Ali over 9 years
    @GusCrawford What is an 'Area'? From msdn.microsoft.com/en-us/library/ee671793(VS.100).aspx: To accommodate large projects, ASP.NET MVC lets you partition Web applications into smaller units that are referred to as areas. Areas provide a way to separate a large MVC Web application into smaller functional groupings. An area is effectively an MVC structure inside an application. An application could contain several MVC structures (areas).
  • Amna Ali
    Amna Ali over 9 years
    As for the other question, I don't think it's related to this answer, and it doesn't seem relevant even to the original question. May be, you should post it somewhere else.
  • Gus Crawford
    Gus Crawford over 9 years
    Ill ask separately in a new thread reply thanks for the perspective.
  • monty
    monty almost 9 years
    To anyone else, to complete this you'll also need to add the namespace to your "Default" route (I found otherwise, although I could get the correct default page, all other links to controllers in the "default" area could not find anything), a la: stackoverflow.com/a/21711800/753471
  • Richard
    Richard almost 9 years
    Works for me. Thanks Amna.
  • Vishnu Vikraman Sreekala
    Vishnu Vikraman Sreekala over 8 years
    This is what I am looking for @Amna Ali