109

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?

Renaissance
  • 798
  • 5
  • 15
Adrian10 BEN
  • 1,283
  • 2
  • 11
  • 15

4 Answers4

170

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 }
);
Imad Alazani
  • 6,688
  • 7
  • 36
  • 58
Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101
32

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.

Nimesh
  • 3,342
  • 1
  • 28
  • 35
  • 16
    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. – Martin Liversage Sep 10 '14 at 16:28
27

I didn't 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" });
Dale K
  • 25,246
  • 15
  • 42
  • 71
Amna Ali
  • 1,775
  • 1
  • 16
  • 19
  • 1
    great question, why don't you create it as one? – Stuart Dobson Apr 22 '14 at 10:26
  • 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? – Gus Crawford Nov 05 '14 at 18:03
  • 2
    @GusCrawford What is an 'Area'? From http://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 Nov 05 '14 at 19:29
  • 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. – Amna Ali Nov 05 '14 at 19:32
  • Ill ask separately in a new thread reply thanks for the perspective. – Gus Crawford Nov 05 '14 at 19:59
  • 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: http://stackoverflow.com/a/21711800/753471 – monty May 18 '15 at 00:33
  • Works for me. Thanks Amna. – Richard Jul 31 '15 at 11:26
  • This is what I am looking for @Amna Ali – Vishnu Vikraman Sreekala Sep 16 '15 at 05:31
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 }
);
Manjoor
  • 4,091
  • 10
  • 43
  • 67