How to create a MVC5 MvcHtmlString in ASP.NET Core

Viewed 9970

I wonder if someone can help with demonstrating how to create a IHtmlContent or HtmlString in ASP.NET Core, similar to what I previously have done in MVC5. I would normally declare a new MvcHtmlString method within a Helper class like so:

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

To use in a View would be @Html.MenuLink("Home", "Home", "Index", "", "Home") also the inclusion of @using WebApplication1.Helpers at the top of the View.

I'm unsure as to the use of HtmlString or IHtmlContent to achieve what I need, but my method requires access to the HttpContextAccessor, but I am a little unsure how to do this.

I have declared the HttpContextAccessor in the Startup.cs as I believe in ASP.NET Core 2.0 it is not declared by default as shown below but require assistance as how to use within the helper method.

Startup.cs

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