4

在 ASP.NET 5 MVC6 RC1 - 我有一个 ViewComponent 旨在代表我传统的“屏幕左侧”主菜单。

我正在编写我的第一个 TagHelper 来表示每个菜单项链接。

我被困在我试图创建超链接的部分。

我该如何解决~/dashboard/summary?

如果我在此页面上显示菜单,则链接显示为/dashboard/~/dashboard/summary

@Url.Content("...")显示@Url.Content("...")ie 未作为剃刀处理。标签助手输出纯。

理想情况下,我希望该解决方案与 .NET Core 兼容,因为我最终的目标是实现 .NET Core 可部署解决方案。

见下文:

namespace Website
{

    /// <summary>
    /// <MainMenuLink area="" controller="" action=""></MainMenuLink>
    /// 
    /// to render
    /// 
    ///  <a href="~/account/manage/ChangePassword" class="list-group-item @GetClassName("manage", "changepassword")">
    ///    <p class="list-group-item-text"><i class="fa fa-terminal"></i>&nbsp;&nbsp;Change my password</p>
    /// </a>
    /// 
    /// 
    /// </summary>
    [HtmlTargetElement(Attributes = "area, controller, action")]
    public class MainMenuLinkTagHelper : TagHelper
    {
        [HtmlAttributeName("area")]
        public string Area { get; set; }

        [HtmlAttributeName("controller")]
        public string Controller { get; set; }

        [HtmlAttributeName("action")]
        public string Action { get; set; }

        public UrlHelper urlHelper { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "a";    // Works    
            // Stuck here - I want ~/ to resolve to the base root.  
            // At the moment the address is here is localhost:XXXXX/dashboard/~/dashboard/summary

            // Would prefer to use a method which can be used with .net core and not System.Web

            output.Attributes.Add("href", "~/dashboard/summary");
            output.Content.SetHtmlContent("Click me");

        }        
        /// <summary>            
    }
}

谢谢!担。

4

2 回答 2

13

默认情况下, ASP Core 2.0似乎不包括在内IActionContextAccessorIServiceCollection因此您需要在启动时注册它才能使接受的解决方案起作用:

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

否则,提供的属性可以将 的属性设置TagHelperViewContext扩展ActionContext

using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;

[HtmlTargetElement("my-tag")]
public class MyTagHelper : TagHelper {

    private readonly IUrlHelperFactory urlHelperFactory;

    [ViewContext]
    [HtmlAttributeNotBound]
    public ViewContext ViewContext { get; set; }

    public MyTagHelper (IUrlHelperFactory urlHelperFactory) {
        this.urlHelperFactory = urlHelperFactory;
    }

    public override void Process(TagHelperContext context, TagHelperOutput output) {
        var urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
    }
}

注意:该ViewContext属性必须有一个公共的 set 方法

于 2018-03-20T16:13:26.790 回答
4

IUrlHelper在标签助手中添加构造函数依赖项。然后使用您可以在视图中使用的相同扩展方法IUrlHelper.Action(actionName, controllerName)来生成 url,例如:

private IUrlHelper urlHelper;
...
public MainMenuLinkTagHelper (IUrlHelper urlHelper)
{
    this.urlHelper = urlHelper;
}
...
public override void Process(TagHelperContext context, TagHelperOutput output)
{
    output.TagName = "a";
    output.Attributes.Add("href", this.urlHelper.Action(this.Action, this.Controller));
    output.Content.SetHtmlContent("Click me");
} 

编辑:ASP 核心版本 1.0

不再可能直接注入IUrlHelper. 您需要同时注入IActionContextAccessorand IUrlHelperFactory,然后IUrlHelper使用它们。例如:

private IUrlHelper UrlHelper => 
    this.urlHelperFactory.GetUrlHelper(this.actionAccessor.ActionContext);
于 2015-12-20T21:47:44.887 回答