How to set Default Controller in asp.net MVC 4 & MVC 5
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 attached 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 }
);
Related videos on Youtube
Comments
-
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 about 9 yearsas would be changing from "/" to "/home as root page? This "/home" must be visible at customer's browser.
-
-
Luis D Urraca about 11 yearsIn MVC 4 the default route is set in the App_Start/RouteConfig.cs
-
Dave Alperovich about 11 yearsVery true. I'm currenlty working on projects in both and overlook these things ;)
-
Good Samaritan almost 11 yearsDave 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 over 10 yearsHow to make that work when the controller referenced in default route sits in another area (i.e. not the root one)?
-
Dave Alperovich over 10 yearsyou should be able to make the default route go to
"AreaName/{controller}/{action}/{id}"
-
Stuart Dobson about 10 yearsgreat question, why don't you create it as one?
-
Martin Liversage over 9 yearsYour "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 over 9 yearsWhat 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 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 over 9 yearsAs 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 over 9 yearsIll ask separately in a new thread reply thanks for the perspective.
-
monty almost 9 yearsTo 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 almost 9 yearsWorks for me. Thanks Amna.
-
Vishnu Vikraman Sreekala over 8 yearsThis is what I am looking for @Amna Ali