Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set "Homepage" in Asp.Net MVC

In asp.net MVC the "homepage" (ie the route that displays when hitting www.foo.com) is set to Home/Index .

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

I tried grepping for Home/Index in my project and couldn't find a reference, nor could I see anything in IIS (6). I looked at the default.aspx page in the root, but that didn't seem to do anything relevent.

Thanks

like image 349
NikolaiDante Avatar asked Jul 17 '09 08:07

NikolaiDante


People also ask

How to change home page in ASP net?

Select Admin Tools --> IIS Manager --> Select your website from the list. Click on Default Document on the right hand side and Click Add . Move the entry to the top of the list using the arrows. You are done.

Which is the startup page in MVC application?

Select the Web tab on the left-hand side. Under the Start Action section, define the Specific Page you would like to default to when the application is launched.


2 Answers

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

You can set up a default route:

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

Just change the Controller/Action names to your desired default. That should be the last route in the Routing Table.

like image 71
Michael Stum Avatar answered Sep 20 '22 00:09

Michael Stum


ASP.NET Core

Routing is configured in the Configure method of the Startup class. To set the "homepage" simply add the following. This will cause users to be routed to the controller and action defined in the MapRoute method when/if they navigate to your site’s base URL, i.e., yoursite.com will route users to yoursite.com/foo/index:

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

Pre-ASP.NET Core

Use the RegisterRoutes method located in either App_Start/RouteConfig.cs (MVC 3 and 4) or Global.asax.cs (MVC 1 and 2) as shown below. This will cause users to be routed to the controller and action defined in the MapRoute method if they navigate to your site’s base URL, i.e., yoursite.com will route the user to yoursite.com/foo/index:

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 }     ); } 
like image 35
JTW Avatar answered Sep 19 '22 00:09

JTW