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?

1
  • as would be changing from "/" to "/home as root page? This "/home" must be visible at customer's browser. Commented Feb 25, 2015 at 14:12

4 Answers 4

171

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 }
);
Sign up to request clarification or add additional context in comments.

5 Comments

In MVC 4 the default route is set in the App_Start/RouteConfig.cs
Very true. I'm currenlty working on projects in both and overlook these things ;)
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.
How to make that work when the controller referenced in default route sits in another area (i.e. not the root one)?
you should be able to make the default route go to "AreaName/{controller}/{action}/{id}"
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.

1 Comment

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

7 Comments

great question, why don't you create it as one?
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?
@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).
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.
Ill ask separately in a new thread reply thanks for the perspective.
|
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 }
);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.