Home > front end >  URLDecode not working when building breadcrumb trail
URLDecode not working when building breadcrumb trail

Time:11-16

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/1 instead of /Clinic/1.

I thought the decode would get rid of that?

Banging my head of the wall.

CodePudding user response:

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 /.

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

  • Related