使用Asp.net Core,C#,Visual 2019。
我正在尝试向我的应用程序添加一个面包屑线索。在网上看到了这个。
我已经将HtmlExtensions.cs添加到扩展文件夹中,并添加了以下代码-
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并添加了以下代码-
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中添加了以下内容-
<!-- #region Breadcrumb -->
@Html.BuildBreadcrumbNavigation();
<!-- #endregion -->但是它不喜欢BuildBreadcrumbNavigation()。
我需要添加引用吗?
以前从未使用过扩展。
谢谢
我被卡住了。不知道该怎么做。
发布于 2022-11-11 12:16:40
弄明白了。一天过得很充实。只需要添加@使用
https://stackoverflow.com/questions/74400682
复制相似问题