gpt4 book ai didi

c# - 如何在 ASP.NET Core 中创建 MVC5 MvcHtmlString

转载 作者:太空狗 更新时间:2023-10-29 22:57:01 31 4
gpt4 key购买 nike

我想知道是否有人可以帮助演示如何在 ASP.NET Core 中创建 IHtmlContent 或 HtmlString,类似于我之前在 MVC5 中所做的。我通常会像这样在 Helper 类中声明一个新的 MvcHtmlString 方法:

HtmlExtender.cs

using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebApplication1.Helpers
{
public static class HtmlExtender
{
public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string controller, string action, string area, string anchorTitle)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);

var url = urlHelper.Action(action, controller, new { area });

var anchor = new TagBuilder("a") {InnerHtml = HttpUtility.HtmlEncode(linkText)};
anchor.MergeAttribute("href", url);
anchor.Attributes.Add("title", anchorTitle);

var listItem = new TagBuilder("li") {InnerHtml = anchor.ToString(TagRenderMode.Normal)};

if (CheckForActiveItem(htmlHelper, controller, action, area))
{
listItem.GenerateId("menu_active");
}

return MvcHtmlString.Create(listItem.ToString(TagRenderMode.Normal));
}

private static bool CheckForActiveItem(HtmlHelper htmlHelper, string controller, string action, string area)
{
if (!CheckIfTokenMatches(htmlHelper, area, "area"))
return false;

if (!CheckIfValueMatches(htmlHelper, controller, "controller"))
return false;

return CheckIfValueMatches(htmlHelper, action, "action");
}
}
}

在 View 中使用 @Html.MenuLink("Home", "Home", "Index", "", "Home") 也包括 @using View 顶部的 WebApplication1.Helpers

我不确定使用 HtmlString 或 IHtmlContent 来实现我的需要,但我的方法需要访问 HttpContextAccessor,但我有点不确定如何执行此操作。

我已经在 Startup.cs 中声明了 HttpContextAccessor,因为我相信 ASP.NET Core 2.0 它不是默认声明的,如下所示,但需要帮助了解如何在辅助方法中使用。

Startup.cs

public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddMvc();
serviceCollection.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

最佳答案

MVC Core 中的新基元非常好用且易于使用。 TagBuilder 实现 IHtmlContent,它可以而且应该在您当前使用 MvcHtmlString 的任何地方使用。对于上面的示例,只需删除 MvcHtmlString.Create 并直接返回 TagBuilder(调整以返回 IHtmlContent)。

其他有用的类是 HtmlContentBuilder,另一种返回 IHtmlContent 的类型,它可以 AppendHtml,类似于 StringBuilder,但专门针对 HTML 内容。您可以附加许多标签构建器,这非常方便。

理论上这就是您所追求的(我在其他地方找到了这个 GetUrlHelper 扩展,我忘了在哪里)。

    public static IUrlHelper GetUrlHelper(this IHtmlHelper html)
{
var urlFactory = html.ViewContext.HttpContext.RequestServices.GetRequiredService<IUrlHelperFactory>();
var actionAccessor = html.ViewContext.HttpContext.RequestServices.GetRequiredService<IActionContextAccessor>();
return urlFactory.GetUrlHelper(actionAccessor.ActionContext);
}
public static IHtmlContent MenuLink(this IHtmlHelper htmlHelper, string linkText, string controller, string action, string area, string anchorTitle)
{

var urlHelper = htmlHelper.GetUrlHelper();

var url = urlHelper.Action(action, controller, new { area });

var anchor = new TagBuilder("a");
anchor.InnerHtml.Append(linkText);
anchor.MergeAttribute("href", url);
anchor.Attributes.Add("title", anchorTitle);

var listItem = new TagBuilder("li");
listItem.InnerHtml.AppendHtml(anchor);

if (CheckForActiveItem(htmlHelper, controller, action, area))
{
listItem.GenerateId("menu_active", "_");
}

return listItem;
}

关于c# - 如何在 ASP.NET Core 中创建 MVC5 MvcHtmlString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45502998/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com