When you create a ASP.NET MVC 2开发者_Python百科 web application in Visual Studio, it gives you a default MVC site. It shows 2 menus [Home] and [About]. When you hover these menus it gives you this URL:
- [Home] localhost:1234/
- [About] localhost:1234/Home/About
Why is About under Home?
I want menu links like
- [Home] localhost:1234/
- [About] localhost:1234/About
How can I do this?
You've got a few options here, you can either map routes individually as Darin and Alexn are showing, or you can do something like this:
routes.MapRoute("Home", "{action}/{id}", new { controller = "Home", action = "index", id = "" });
See how there's no controller defined in the path? This should mean you can just say /About
or /SomeOtherAction
. The limitation here is that it's going to match a lot of routes, and they'll all go to the home controller unless you add more specific routes above.
About is under Home because it's an action under the HomeController.
If you want to access about using /About, set up a new route in your Global.asax. Example:
routes.MapRoute("About", "About", new { controller = "Home", action = "About" }
You have two actions defined on the Home
controller: Index
and About
. The Index action is defined as the default action for the site. For this reason http://localhost:1234
resolves to http://localhost:1234/Home/Index
. You could set a new route to accomplish this:
routes.MapRoute(
"About",
"About",
new { controller = "Home", action = "About", id = UrlParameter.Optional }
);
Putting about under any folder is a matter of personal choice.
If you would like to see the about file in a different folder structure the neat way of doing this in my opinion would be
1)Create a new folder in views called About. 2)Move the About.html file into this folder 3)Create a new file under Controllers folder called AboutController.cs 4) find the tag return View(); in the above created file and replace it with return View("About");
If you would like to add more files under About section(eg:about-autor.html,about-fundinghouse.html) you could continue to add it in the about folder and make the necessary change in the controller to return the respective view.
精彩评论