Install Homepage in Asp.Net MVC

In asp.net MVC, the "home page" (i.e. the route displayed when it hits www.foo.com) is set to "Home / Index".

  • Where is this value stored?
  • How can I change the "home page"?
  • Is there anything more elegant than using RedirectToRoute () in the Index action of the main controller?

I tried grepping for Home / Index in my project and could not find the link, and could not see anything in IIS (6). I looked at the default.aspx page in the root, but that didn't seem to mean anything.

thank

+96
c # asp.net-mvc asp.net-mvc-routing
Jul 17 '09 at 8:22
source share
8 answers

Look at Default.aspx/Default.aspx.cs and the file Global.asax.cs

You can configure the default route:

  routes.MapRoute( "Default", // Route name "", // URL with parameters new { controller = "Home", action = "Index"} // Parameter defaults ); 

Just change the name of the controller / action to the default one. This should be the last route in the routing table.

+145
Jul 17 '09 at 8:27
source share

ASP.NET Kernel

Routing is configured in the Configure method of the Startup class. To set the home page, simply add the following. This will lead to the fact that users will be redirected to the controller and the action defined in the MapRoute method when / if they move to the base URL of your site, i.e. Your site will route users to your .com / foo / index site:

 app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=FooController}/{action=Index}/{id?}"); }); 

Kernel Pre-ASP.NET

Use the RegisterRoutes method located in App_Start / RouteConfig.cs (MVC 3 and 4) or Global.asax.cs (MVC 1 and 2), as shown below. This will result in users being redirected to the controller and the action defined in the MapRoute method if they navigate to the base URL of your site, i.e. Your site will redirect the user to your .com / foo / index site:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // Here I have created a custom "Default" route that will route users to the "YourAction" method within the "FooController" controller. routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "FooController", action = "Index", id = UrlParameter.Optional } ); } 
+16
Apr 26 '14 at 18:05
source share
 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Your Controller", action = "Your Action", id = UrlParameter.Optional } ); } } 
+4
Jul 28 '15 at 9:34
source share

check the RegisterRoutes method in global.asax.cs - this is the default place to configure the route ...

+3
Jul 17 '09 at 8:26
source share

Step 1. Click the Global.asax file in your solution.

Step 2: Then go to the definition

RouteConfig.RegisterRoutes(RouteTable.Routes);

Step 3: Change the controller name and view name

 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 
+3
Aug 07 '16 at 16:05
source share

Attribute Routing in MVC 5

Prior to MVC 5, you could map URLs to specific actions and controllers by calling routes.MapRoute(...) in the RouteConfig.cs file. This is where the URL of the homepage ( Home/Index ) is stored. However, if you change the default route as shown below,

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

remember that this will affect the URLs of other actions and controllers. For example, if you had a controller class named ExampleController and an action method inside it called DoSomething , then the expected default ExampleController/DoSomething will no longer work because the default route has been changed.

The workaround for this is to not spoil the default route and create new routes in the RouteConfig.cs file for other actions and controllers, for example,

 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Example", url: "hey/now", defaults: new { controller = "Example", action = "DoSomething", id = UrlParameter.Optional } ); 

Now the DoSomething action of the ExampleController class will be mapped to the hey/now URL. But this can be tedious to do every time you want to define routes for different activities. So in MVC 5, you can now add attributes to match URLs with such actions,

 public class HomeController : Controller { // url is now 'index/' instead of 'home/index' [Route("index")] public ActionResult Index() { return View(); } // url is now 'create/new' instead of 'home/create' [Route("create/new")] public ActionResult Create() { return View(); } } 
+2
Jun 05 '17 at 14:26
source share

I tried to answer, but it did not work for me. Here is what I did:

Create a new DefaultController. In the index action, I wrote one line redirect:

 return Redirect("~/Default.aspx") 

In RouteConfig.cs, change the controller = "Default" for the route.

  routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional } ); 
+1
Jul 02 '16 at 1:43
source share

If you do not want to change the router, just go to HomeController and change MyNewViewHere in the index as follows:

  public ActionResult Index() { return View("MyNewViewHere"); } 
0
Apr 16 '19 at 3:26
source share



All Articles