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

时间:2017-08-04 09:22:11

标签: 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");
        }
    }
}

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

我不确定使用HtmlString或IHtmlContent来实现我需要的东西,但我的方法需要访问HttpContextAccessor,但我有点不确定如何做到这一点。

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

Startup.cs

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

1 个答案:

答案 0 :(得分:4)

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;
    }