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

igg*_*web 6 c# asp.net-mvc-5 asp.net-core-mvc

我想知道是否有人可以帮助演示如何在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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在视图中使用@Html.MenuLink("Home", "Home", "Index", "", "Home")也将包含@using WebApplication1.Helpers在视图顶部。

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

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

启动文件

public void ConfigureServices(IServiceCollection serviceCollection)
{
    serviceCollection.AddMvc();
    serviceCollection.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
Run Code Online (Sandbox Code Playgroud)

ocd*_*cdi 8

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

其他有用的类是HtmlContentBuilder,它是返回IHtmlContent的另一种类型,可以附加ApptHtml,类似于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;
    }
Run Code Online (Sandbox Code Playgroud)