0

I am using ASP.Net Core 5 MVC Visual Studio 2019.

I am building a buildcrumb trail.

My Code is -

  var breadcrumb = new HtmlContentBuilder()
                                .AppendHtml("<ol class='breadcrumb'><li>")
                                .AppendHtml(helper.ActionLink(ob.breadcrumbmap.YFVCList.Name.Titleize(),ob.breadcrumbmap.YFVCList.Action, ob.breadcrumbmap.YFVCList.Controller))
                                .AppendHtml("</li>"

  if (controllerName.ToLower() != ob.breadcrumbmap.YFVCList.Controller.ToLower())
  {
     breadcrumb.AppendHtml("<li class='breadcrimb-item'>")
                    .AppendHtml(helper.ActionLink(ob.breadcrumbmap.YFVCList.YFVC.Name.Titleize(), System.Web.HttpUtility.UrlDecode(ob.breadcrumbmap.YFVCList.YFVC.Action + "/1"), ob.breadcrumbmap.YFVCList.YFVC.Controller))
        .AppendHtml("</l>");
  }

I get my info from a JSON file which I put into a object. -

 string jsonData = File.ReadAllText("BreadcrumbMap.json");
 Rootobject ob = JsonSerializer.Deserialize<Rootobject>(jsonData);

ob.breadcrumbmap.YFVCList.YFVC.Action = "Clinic"

But I need to append an id on the end so I use + "/1" and UrlDecode the string.

System.Web.HttpUtility.UrlDecode(ob.breadcrumbmap.YFVCList.YFVC.Action + "/1")

However when I highlight the breadcrumb it shows -

/Clinic%2F1 instead of /Clinic/1.

I thought the decode would get rid of that?

Banging my head of the wall.

  • `controllerName.ToLower() != ob.breadcrumbmap.YFVCList.Controller.ToLower()` <-- _le sigh_ – Dai Nov 14 '22 at 16:13
  • I don't understand as that has nothing to do with what I am asking! – user20408154 Nov 14 '22 at 16:17
  • Why are you using `UrlDecode` in the first place? You don't `UrlEncode` nor `UrlDecode` _an entire URL_: it's intended for encoding query parameters.` – Dai Nov 14 '22 at 16:17
  • Your inappropriate use of `.ToLower()` is not relevant to the problem, but it annoys me immensely when I see people thinking that's how you perform a case-insensitive string comparison (hint: it isn't). Use `!controllerName.Equals( ob.breadcrumbmap.YFVCList.Controller, StringComparison.OrdinalIgnoreCase )` instead. – Dai Nov 14 '22 at 16:19
  • I removed the UrlDecode and it still came up with the %2F instead of /. Thanks Dai, I'll change my if statement. – user20408154 Nov 14 '22 at 16:34

1 Answers1

0

You seems to misunderstand the HTML.ActionLink method. From your code and description, I think you want to generate a route in this template:

{ControllerName/ActionName/Routevalue}

And in your code, You use this override method:

 // Summary:
        //     Returns an anchor (<a>) element that contains a URL path to the specified action.
        //
        // Parameters:
        //   helper:
        //     The Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper instance this method extends.
        //
        //   linkText:
        //     The inner text of the anchor element. Must not be null.
        //
        //   actionName:
        //     The name of the action.
        //
        //   controllerName:
        //     The name of the controller.
        //
        // Returns:
        //     A new Microsoft.AspNetCore.Html.IHtmlContent containing the anchor element.
        public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            throw null;
        }

System.Web.HttpUtility.UrlDecode(ob.breadcrumbmap.YFVCList.YFVC.Action + "/1") is considered a whole action name instead of action/route, So it will convert / to %2F.

You can use this override method:

public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues)
{
      throw null;
}

write your code like:

.AppendHtml(helper.ActionLink(ob.breadcrumbmap.YFVCList.YFVC.Name.Titleize(), ob.breadcrumbmap.YFVCList.YFVC.Action, ob.breadcrumbmap.YFVCList.YFVC.Controller), new { id=2 }))

Then it will generate url like this:

enter image description here

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
  • 1
    Thanks. Works now with your help. I didn't override the method, I used - `helper.ActionLink(ob.breadcrumbmap.YFVCList.YFVC.Name.Titleize(), ob.breadcrumbmap.YFVCList.YFVC.Action, ob.breadcrumbmap.YFVCList.YFVC.Controller, new {id=1}, null))` and jus passed in null for htmlattributes. Wouldn't have found it without your help @Xinran Shen – user20408154 Nov 15 '22 at 09:41