108

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

Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
NikolaiDante
  • 18,469
  • 14
  • 77
  • 117

8 Answers8

160

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • 6
    @NikolaiDante you should make that comment an answer as I nearly missed it and it's quicker than this answer. :) Thanks – GazB May 21 '13 at 12:20
  • 3
    In MVC 5. if you have a form login, when you click login on the home page, it will then still redirect to Home controller , not your custom controller specified in the route. register action will do similar thing. So apart from changing routeconfig, also need to change some code where calling RedirectionToAction("Index","Home") and replace it with your own controller and action names. – anIBMer Nov 02 '14 at 13:44
  • It's worth pointing out that you can have **Multiple Routes**. This could be your default with BLANK URL parameters, but you probably want a second route like `url: "{controller}/{action}/{id}"`. Just give the routes different names. – Jess Mar 24 '16 at 13:47
  • This answer may work for you, but if you need multiple routes, see here http://stackoverflow.com/a/8470531/1804678 – Jess Mar 24 '16 at 14:10
  • 1
    This answer is only suitable for MVC 3 and earlier. See my answer below for the recommended MVC 4 and later approach. – JTW Jul 03 '16 at 16:29
  • This file is now located in the `App_Start` folder under the name `RouteConfig.cs` – jjasspper Jan 05 '20 at 17:44
26

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 }
    );
}
JTW
  • 3,546
  • 8
  • 35
  • 49
5

Step 1: Click on Global.asax File in your Solution.

Step 2: Then Go to Definition of

RouteConfig.RegisterRoutes(RouteTable.Routes);

Step 3: Change 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 }
                        );
    }
}
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Ankur Shah
  • 467
  • 5
  • 3
4
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 }
        );
    }
}
benka
  • 4,732
  • 35
  • 47
  • 58
Niraj
  • 41
  • 5
4

Attribute Routing in MVC 5

Before 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 for the homepage is stored (Home/Index). However if you modify the default route as shown below,

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

keep in mind 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 of it called DoSomething, then the expected default url ExampleController/DoSomething will no longer work because the default route was changed.

A workaround for this is to not mess with the default route and create new routes in the RouteConfig.cs file for other actions and controllers like so,

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 url hey/now. But this can get tedious to do for every time you want to define routes for different actions. So in MVC 5 you can now add attributes to match urls to actions like so,

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();
    }
}
Community
  • 1
  • 1
Chris Gong
  • 8,031
  • 4
  • 30
  • 51
3

check RegisterRoutes method in global.asax.cs - it's the default place for route configuration...

Michał Chaniewski
  • 4,646
  • 1
  • 18
  • 15
1

I tried the answer but it didn't worked for me. This is what i ended up doing:

Create a new controller DefaultController. In index action, i wrote one line redirect:

return Redirect("~/Default.aspx")

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

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
        );
vickey1611
  • 79
  • 1
  • 1
  • you probably forgot to omit "Controller" word in the name of controller, creating default route – amarax Jun 08 '18 at 15:20
0

If you don't want to change the router, just go to the HomeController and change MyNewViewHere in the index like this:

    public ActionResult Index()
    {
        return View("MyNewViewHere");
    }