我想知道是否有人能帮助演示如何在IHtmlContent核心中创建一个HtmlString或ASP.NET,就像我以前在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中声明了Startup.cs,就像我在ASP.NET Core2.0中所相信的一样,在默认情况下,它不是像下面所示的那样声明的,而是需要帮助作为在帮助器方法中使用的方法。
Startup.cs
public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddMvc();
serviceCollection.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}发布于 2017-12-18 21:44:33
MVC Core中的新原语很好,而且易于使用。TagBuilder实现了IHtmlContent,它可以而且应该在当前使用MvcHtmlString的任何地方使用。对于上面的示例,只需删除MvcHtmlString.Create并直接返回TagBuilder (调整为返回IHtmlContent)。
其他有用的类是HtmlContentBuilder (返回IHtmlContent的另一种类型),它可以AppendHtml,类似于StringBuilder,但具体用于IHtmlContent。您可以附加许多标记构建器,这非常方便。
从理论上讲,这就是您所追求的(我在其他地方找到了这个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;
}https://stackoverflow.com/questions/45502998
复制相似问题