ASP .net MVC Invoking default controller and action vs Setting a startup page
I am 开发者_如何转开发developing code on the sample ASP .net MVC template provided by VS2010. The first time I ran the code without adding anything, the index.aspx page was invoked which is expected. But for some reasons I added a login.aspx and then accidentally set that as a startup page. Now when I ran the application the default startup url look like http://localhost/Views/login.aspx. I am thinking this is not a valid MVC routing path and I get the requested resource cannot be found error.
I am not sure how to revert this back and make sure the default ../home/index is invoked. Can any one throw some light on this? Also should I not set the startup page as we do in asp .net webforms?
Short answer: You have done something that you are allowed to do in ASP.NET WebForms but it doesn't work the same in ASP.NET MVC. To revert what you have done, please open project properties (From Project Menu) and go to the "Web" tab. Set the Start Action to Current Page instead of Specific Page. Now when you run your project, you will be able to see the Home/Index page as before.
To do it properly in MVC, you need to configure the route in Global.asax. You will find an entry similar to the following:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
You can see that that when you don't specify a controller and action, it goes to Home/Index. You can change the controller name and action name there and it will default to the action you have mentioned there. Just make sure that you have the relevant action in the controller that you have specified there.
精彩评论