문제

Please help me to correct my ASP.NET MVC Route Map.

I've got a menu item with an ActionLink:
<li>@Html.ActionLink("Articles", "List", "Article")</li>

On the Home page it looks like: localhost/Article which is OK.

But on the concrete Article page with URL localhost/Article/List/11 my menu link is the same: localhost/Article/List/11

But I need localhost/Article

My route map code is as follows:

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 }
    );
}

Here is controller code:

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();
    }
도움이 되었습니까?

해결책

my problem was solved by changing my route map:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
               name: "ArticleByFirst",
               url: "Article/{action}/",
               defaults: new { controller = "Article", action = "List" }
           );

            routes.MapRoute(
                name: "ArticleById",
                url: "Article/List/{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 }
            );
        }

And my menu-link looks like: <li><a href="@Url.RouteUrl("ArticleByFirst", new { action = "List" })">Articles</a></li>

And my item-link is: <li><a href="@Url.RouteUrl("ArticleById", new { id = item.Id })">@item.Name</a></li>

It's not good, that I can't choose name of Route Map in ActionLink.

다른 팁

I guess your current querystrings items are being added to your action link. To avoid this problem, Use this overload and try to explicitly pass null as RouteValues and see what changes it brings.

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    RouteValueDictionary routeValues,
    IDictionary<string, Object> htmlAttributes
)

So your code can be re-written as

<li>@Html.ActionLink("Articles", "List", "Article",
                                        new RouteValueDictionary { },null)</li>

You can also try this version also

<a href="Url.Action("List", "Articles")">Articles</a>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top