问题
I'm using ASP.NET MVC 4 and I have some problems settings up my routes. Could you tell my how to set up my routes to point urls to actions as follows:
- "/" (or "/Start") => PublicController.Start()
- "/About" => PublicController.About()
- "/MyPage" (or "/MyPage/Summary") => MyPageController.Summary()
- "/MyPage/Invoices" => MyPageController.Invoices()
- "/MyPage/Invoice/72" => MyPageController.Invoice(int id)
It's the url "/About" that messes things up for me, i.e. a url that does not specify the controller. If I make that one work the others that do specify controller stop working. I could just create a separate controller for "/About" I guess, but I'd rather not if I don't have to (I have more urls following that pattern).
回答1:
This should do it:
routes.MapRoute(
name: "About",
url: "About",
defaults: new { controller = "Public", action = "About" }
);
routes.MapRoute(
name: "MyPageSummary",
url: "MyPage",
defaults: new { controller = "MyPage", action = "Summary" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Public", action = "Start", id = UrlParameter.Optional }
);
来源:https://stackoverflow.com/questions/15836401/routing-with-and-without-controller-name-in-asp-net-mvc-4