Home > Mobile >  Problem With HtmlExtensions (New to) and Breadcrumb Trail
Problem With HtmlExtensions (New to) and Breadcrumb Trail

Time:11-15

Using Asp.net Core, C#, Visual Studio 2019.

I am trying to add a breadcrumb trail to my application. Saw this on the net.

I have added HtmlExtensions.cs to the extensions folder and added the following code -

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace YellowFeverPortal.Web.Extensions
{
    public static class HtmlExtensions
    {
        private static readonly HtmlContentBuilder _emptyBuilder = new HtmlContentBuilder();

        public static IHtmlContent BuildBreadcrumbNavigation(this IHtmlHelper helper)
        {
            if (helper.ViewContext.RouteData.Values["controller"].ToString() == "Home" ||
                helper.ViewContext.RouteData.Values["controller"].ToString() == "Account")
            {
                return _emptyBuilder;
            }

            string controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
            string actionName = helper.ViewContext.RouteData.Values["action"].ToString();

            var breadcrumb = new HtmlContentBuilder()
                                .AppendHtml("<ol class='breadcrumb'><li>")
                                .AppendHtml(helper.ActionLink("Home", "Index", "Home"))
                                .AppendHtml("</li><li>")
                                .AppendHtml(helper.ActionLink(controllerName.Titleize(),
                                                          "Index", controllerName))
                                .AppendHtml("</li>");


            if (helper.ViewContext.RouteData.Values["action"].ToString() != "Index")
            {
                breadcrumb.AppendHtml("<li>")
                          .AppendHtml(helper.ActionLink(actionName.Titleize(), actionName, controllerName))
                          .AppendHtml("</li>");
            }

            return breadcrumb.AppendHtml("</ol>");
        }
    }
}

I also created a StingsExtensions.cs and added the following code -

using System.Globalization;
using System.Text.RegularExpressions;


namespace YellowFeverPortal.Web.Extensions
{
    public static class StringExtensions
    {
        public static string Titleize(this string text)
        {
            return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text).ToSentenceCase();
        }

        public static string ToSentenceCase(this string str)
        {
            return Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0]   " "   char.ToLower(m.Value[1]));
        }
    }
}

And then I added the following to _Layout.cshtml -

<!-- #region Breadcrumb -->
@Html.BuildBreadcrumbNavigation();
<!-- #endregion -->

But it doesn't like BuildBreadcrumbNavigation().

Do I need to add a reference?

Never used extensions before.

Thanks

I am stuck. Don't know what to do.

CodePudding user response:

Figured it out. Having a bit of a thick day. Just needed to add @using

  • Related