首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HtmlExtensions (New )和Breadcrumb的问题

HtmlExtensions (New )和Breadcrumb的问题
EN

Stack Overflow用户
提问于 2022-11-11 09:49:59
回答 1查看 20关注 0票数 0

使用Asp.net Core,C#,Visual 2019。

我正在尝试向我的应用程序添加一个面包屑线索。在网上看到了这个。

我已经将HtmlExtensions.cs添加到扩展文件夹中,并添加了以下代码-

代码语言:javascript
复制
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>");
        }
    }
}

我还创建了一个StingsExtensions.cs并添加了以下代码-

代码语言:javascript
复制
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]));
        }
    }
}

然后我在_Layout.cshtml中添加了以下内容-

代码语言:javascript
复制
<!-- #region Breadcrumb -->
@Html.BuildBreadcrumbNavigation();
<!-- #endregion -->

但是它不喜欢BuildBreadcrumbNavigation()。

我需要添加引用吗?

以前从未使用过扩展。

谢谢

我被卡住了。不知道该怎么做。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-11 12:16:40

弄明白了。一天过得很充实。只需要添加@使用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74400682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档