Change default view in VS 2010

2019-05-26 23:57发布

问题:

How to set change a default View in MVC3 when debbuging the project in Visual Studio 2010.

As soon I hit F5, The default View it opens is Localhost/Home/Index.

Where is it being set, How do I update it?

Can anyone shed some light on this please? It is not straight forward(for me though).

Thank you

回答1:

All you have to do is changed your default MapRoute parameters. Typically, this is what you'll see by default as your Default route:

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

Just change the controller property and the action property to what you want your default to be. For instance, you could do:

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "AnotherController", 
                  action = "aDifferentAction", 
                  id = UrlParameter.Optional }); // Parameter defaults

All that is changed here is the controller and action properties. Now when you browse to just the qualified name, it will go to your AnotherController.aDifferentAction() method, instead of the HomeController.Index() method.

Explanation

The reason why it defaults to Home.Index(), is because that is the first matched route when you have empty route parameters for controller and action. By changing these defaults in the MapRoute() call, you are telling routing that if nothing is there for the route parameters, go to AnotherController.aDifferentAction() action method.

As long as this is the first route, you should be set.



回答2:

You can set the default page in the Routes table as Shark suggests, but something tells me this probably isn't really what you're looking for. If you just want to debug a specific page, right-click the view and select 'View In Browser' from the context menu.