0

I have route in RouteConfig:

routes.MapRoute(
    name: "AdsCategories",
    url: "Ads/{key}",
    defaults: new 
    { 
        controller = "Ads", 
        action = "Index", 
        key = UrlParameter.Optional 
    },
    constraints: new
    {
        key = new ExcludingValuesConstraint(
                  "edit", "create", "details",
                  "delete", "AdsSlider", "UserAds", 
                  "GetCitiesByRegion", "Index", "AddComent",
                  "search", "SetFilter")
    },
    namespaces: new string[] { "Vprok.Controllers" }
);

And the code that forms a link in htmlhelper:Link.MergeAttribute("href", urlHelper.Action(action, controller));

And if i go to page Ads/somekey. And use on this page helper: @Html.ActiveLi("Объявления", "Index", "Ads"). That return "Ads/somekey" instead "Ads/". Why UrlHelper.Action merged {key} parametr whith URL ?

Problem Solved. Tthis code return "Ads/": Link.MergeAttribute("href",urlHelper.RouteUrl("Default", new{ action, controller}));

Buboon
  • 405
  • 5
  • 15

1 Answers1

0

This happens because the current route values are reused when UrlHelper.Action generates an outbound url. You can supply empty route values to the UrlHelper.Action method overriding the unwanted ones.

Also, see this similar question.

Community
  • 1
  • 1
m1kael
  • 2,801
  • 1
  • 15
  • 14