0

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();
    }
Yuriy Mayorov
  • 961
  • 4
  • 15
  • 32
  • Do you need the id in the url? Could you take that off the Article MapRoute. Also you could try setting the RouteValueDictionary indicated by Shyju to new { id = null } instead of just new {} – dreza Mar 22 '13 at 20:31
  • Yes, I need Id in my url. article/list/id -show articles list and selected article. Article/list - show list of articles and last article by date. I know, that I can resolve my problem by adding new action like "view". Article/view/id. But I am interested to use only one action:) – Yuriy Mayorov Mar 23 '13 at 08:24
  • Is the int a Nullable int into your controller action i.e. int? id. Perhaps this might also have an effect combined with the new { id = null } of the ActionLink() method – dreza Mar 23 '13 at 20:33
  • @dreza Yes, I adde my controller code into the post – Yuriy Mayorov Mar 24 '13 at 18:59

2 Answers2

1

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.

Yuriy Mayorov
  • 961
  • 4
  • 15
  • 32
0

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>
Shyju
  • 214,206
  • 104
  • 411
  • 497