0

I have same @Html.ActionLink in two parts of .net core mvc application but why they are rendering differently?

Startup :

endpoints.MapControllerRoute(
name: "products",
pattern: "product-category/{title}",
defaults: new { controller = "ProductCategories", action = "ProductsRelatedToCategory", pg = 1 });

Action :

public async Task<IActionResult> ProductsRelatedToCategory(int? cat, int? pg)
{
    // using cat to get which category is clicked and pg to get which page it is (in paginated list)
}

in _Layout view:

@Html.ActionLink(
    linkText,
    "ProductsRelatedToCategory",
    "ProductCategories",
    null,
    null,
    null,
    new { cat = 14, pg = 1, title = "power-tools" },
    new { @class = "menuProductLink" })

Renders right as expected :

"url 1" : {domain}/product-category/power-tools?cat=14

in controller's view (ProductsRelatedToCategory.cshtml) used as next page link in pagination:

@Html.ActionLink(
    "Next",
    "ProductsRelatedToCategory",
    "ProductCategories",
    null,
    null,
    null,
    new { cat = productCategory.Id, pg = currentPage + 1, title = "power-tools" },
    new { @class = "paginate" })

Renders not right as expected :

"url 2" : {domain}/ProductCategories/ProductsRelatedToCategory?cat=14&pg=2&title=power-tools

Both url's are working fine but actually I want to display url like "url 1", why they are rendered different?

Mehdi
  • 499
  • 1
  • 7
  • 31
  • So you mean you want to drop `title=power-tools` from the url, but keep the `pg`? – Stefan Sep 07 '20 at 10:29
  • Are you using areas? – mjwills Sep 07 '20 at 10:34
  • What version of ASP.NET Core are you using? I've tested your code using v3.1 (using the route data from example 1 only as you haven't shared your models) and it works as expected i.e. the ActionLink in the view is the same as the one in _Layout.cshtml. Have you tried using the same route data in example 2 just to rule that out? – Glynn Hurrell Sep 07 '20 at 10:52
  • @Stefan No I don't, I need it because my route is customized to show title in display url. I need to pass title, pg and id to controller but title will be in url route and pg,id will be as parameters so it will looks like : domain/product-category/title?id=1&pg=1 – Mehdi Sep 07 '20 at 12:31
  • @mjwills No I did not, and don't know if it will help or not. Please point to an example, thanks – Mehdi Sep 07 '20 at 12:33
  • @GlynnHurrell I'm using 3.1 too, but not working for me. Is there any difference in route patterns in startup in your sample code? – Mehdi Sep 07 '20 at 12:34
  • Areas would _cause_ what you are seeing... – mjwills Sep 07 '20 at 13:05
  • Hi @Mehdi,it could work well in my project.Did you use Area?Could you share a github repository which could reproduce your issue? – Rena Sep 09 '20 at 02:48

0 Answers0