Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 324
Adrian10 BEN Avatar asked Jan 20 '13 16:01

Adrian10 BEN


People also ask

How do I set a default controller?

Defining a Default Controller To specify a default controller, open your application/config/routes. php file and set this variable: $route['default_controller'] = ' Blog '; Where Blog is the name of the controller class you want used.

What is the default action for a controller?

By default, the Index() method is a default action method for any controller, as per configured default root, as shown below. routes. MapRoute( name: "Default", url: "{controller}/{action}/{id}/{name}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.

What is the default return type of the controller in ASP.NET MVC?

ActionResult is a return type of a controller method in ASP.NET MVC.


2 Answers

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 } ); 
like image 198
Dave Alperovich Avatar answered Sep 19 '22 18:09

Dave Alperovich


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.

like image 35
Nimesh Avatar answered Sep 18 '22 18:09

Nimesh