0

我想在 asp.net 5 1.0.0-rc1-final 的菜单链接中生成 class="active",以突出显示 _Layout.cshtml 中导航栏的活动菜单。
这里提出的解决方案:Prashant Adepu 的原始帖子在 Asp.Net.MVC 6.0.0 beta5 中运行良好。但是在6.0.0 rc1 (asp.net 1.0.0 rc-1 final)中,似乎不可能使用 [ViewContext]装饰,因为该属性不存在。
有没有解决的办法?

1) 下面是对 rc-1 稍作修改的代码。除了被拒绝的 [ViewContext]
之外,一切正常。如果没有此属性,viewContext 在运行时将为空)。 2)要运行它,您应该创建一个 asp.net5 WebApplicationX,并添加_ViewImports.cshtml。 然后只需使用而不是常规的 mvc Anchor。
@addTagHelper "WebApplicationX.TagHelpers.MenuLinkTagHelper, WebApplicationX"
<menulink controller-name="Home" action-name="About" menu-text="About"></menulink><a asp-controller="Home" asp-action="Index">Home</a>

using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
using Microsoft.AspNet.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApplicationX.TagHelpers
{
    [HtmlTargetElement("menulink", Attributes = "controller-name, action-name, menu-text")]
    public class MenuLinkTagHelper : TagHelper 
    {
        public string ControllerName { get; set; }
        public string ActionName { get; set; }
        public string MenuText { get; set; }
        [ViewContext]                         //*** This is not allowed.***
        public ViewContext ViewContext { get; set; }

        public IUrlHelper _UrlHelper { get; set; }

        public MenuLinkTagHelper(IUrlHelper urlHelper)
        {
            _UrlHelper = urlHelper;
        }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            StringBuilder sb = new StringBuilder();

            string menuUrl = _UrlHelper.Action(ActionName, ControllerName);

            output.TagName = "li";

            var a = new TagBuilder("a");
            a.MergeAttribute("href", $"{menuUrl}");
            a.MergeAttribute("title", MenuText);
            a.InnerHtml.Append(MenuText);

            var routeData = ViewContext.RouteData.Values;
            var currentController = routeData["controller"];
            var currentAction = routeData["action"];

            if (String.Equals(ActionName, currentAction as string, StringComparison.OrdinalIgnoreCase)
                && String.Equals(ControllerName, currentController as string, StringComparison.OrdinalIgnoreCase))
            {
                output.Attributes.Add("class", "active");
            }

            output.Content.SetContent(a.ToString());
        }
    }
}
4

2 回答 2

2

ViewContext 属性仍然存在。它在 Microsoft.AspNet.Mvc.ViewFeatures 中,而不是在 Microsoft.AspNet.Mvc.Rendering(ViewContext 类本身所在的位置)中。确保选择正确的。你可以在这里查看一个例子:https ://github.com/DannyvanderKraan/TagHelpers

于 2016-03-10T10:05:47.183 回答
0

感谢 Danny 的帮助,在重新访问原版后,您可以在下面找到 1.0.0-rc1-final 的工作代码

    using Microsoft.AspNet.Mvc;
    using Microsoft.AspNet.Mvc.Rendering;
    using Microsoft.AspNet.Mvc.ViewFeatures;                    //change for 1.0.0-rc1.0-final
    using Microsoft.AspNet.Razor.TagHelpers;
    using System;
    using System.Text;

namespace WebAppName.TagHelpers
{
    [HtmlTargetElement("menulink", Attributes = "controller-name, action-name, menu-text")]
    public class MenuLinkTagHelper : TagHelper
    {
        public string ControllerName { get; set; }
        public string ActionName { get; set; }
        public string MenuText { get; set; }
        [ViewContext]
        public ViewContext ViewContext { get; set; }

        public IUrlHelper _UrlHelper { get; set; }

        public MenuLinkTagHelper(IUrlHelper urlHelper)
        {
            _UrlHelper = urlHelper;
        }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            StringBuilder sb = new StringBuilder();

            string menuUrl = _UrlHelper.Action(ActionName, ControllerName);

            output.TagName = "li";

            var a = new TagBuilder("a");
            a.MergeAttribute("href", $"{menuUrl}");
            a.MergeAttribute("title", MenuText);
            a.InnerHtml.Append(MenuText);                    //change for 1.0.0-rc1.0-final

            var routeData = ViewContext.RouteData.Values;
            var currentController = routeData["controller"];
            var currentAction = routeData["action"];

            if (String.Equals(ActionName, currentAction as string, StringComparison.OrdinalIgnoreCase)
                && String.Equals(ControllerName, currentController as string, StringComparison.OrdinalIgnoreCase))
            {
                output.Attributes.Add("class", "active");
            }
            output.Content.Append(a);                    //change for 1.0.0-rc1.0-final
        }
    }
}
于 2016-03-10T16:34:35.163 回答