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();
    }
4
  • 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 {} Commented Mar 22, 2013 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:) Commented Mar 23, 2013 at 8: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 Commented Mar 23, 2013 at 20:33
  • @dreza Yes, I adde my controller code into the post Commented Mar 24, 2013 at 18:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

3 Comments

in this case url of menu is /Home/List?Length=7
@YuriyMayorov: Sorry.It was a wrong overload. Try the updated one.
can u share a sample project ? on skydrive ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.