请帮助我更正我的 ASP.NET MVC 路线图。
我有一个带有 ActionLink 的菜单项:
<li>@Html.ActionLink("Articles", "List", "Article")</li>
在主页上,它看起来像:
localhost/Article
没关系。
但是在带有 URL 的具体文章页面上,localhost/Article/List/11
我的菜单链接是相同的:
localhost/Article/List/11
但是我需要localhost/Article
我的路线图代码如下:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Article",
url: "Article/{action}/{id}",
defaults: new { controller = "Article", action = "List", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
这是控制器代码:
public ActionResult List(int? id)
{
ArticlesDataManager artMgr = new ArticlesDataManager();
ArticleViewModel art = new ArticleViewModel();
art.Articles = artMgr.GetLastArticles();
art.Article = (id == null) ? artMgr.GetLast() : artMgr.GetArticle((int)id);
ViewData.Model = art;
return View();
}