【问题标题】:How to create Html.ActionLink for Current URL?如何为当前 URL 创建 Html.ActionLink?
【发布时间】:2022-12-19 21:05:59
【问题描述】:

使用 ASP.NET MVC 5 和 Bootstrap 3.2,我正在尝试为当前 URL 创建一个 Html ActionLink,就像这里的 Bootstrap 示例一样:

https://www.quackit.com/bootstrap/bootstrap_3/tutorial/bootstrap_breadcrumbs.cfm

<ul class="breadcrumb">
<li><a href="#">Home</a></li>
<li><a href="#">Fruit</a></li>
<li class="active">Pears</li>
</ul>

Microsoft 在他们的 ActionLink 页面上有很多信息

https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.linkextensions.actionlink?view=aspnet-mvc-5.2

似乎没有用于创建活动列表项的版本,除非我在这些重载中遗漏了一些东西。

那么,我如何为上面的无序列表中的“Pears”创建一个 ActionLink(没有超链接和 class="active")?

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    解决方案是:

    如果您不想要超链接,请不要使用 ActionLink

    我仍然为 Home 和 Fruit 使用 ActionLink,但是 Pears 只是得到了标准的 &lt;li&gt; 处理,如下所示:

    public static string BuildBreadcrumbNavigation(this HtmlHelper helper)
    {
        var result = string.Empty;
        var controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
        // optional condition: I didn't wanted it to show on home and account controller
        if ((controllerName != "Home") && (controllerName != "Account"))
        {
            var homeLink = helper.ActionLink(
                linkText: "Home",
                actionName: "Index",
                controllerName: "Home").ToHtmlString();
            var sb = new StringBuilder($"<ol class='breadcrumb'><li>{homeLink}</li>");
    
            var url = HttpContext.Current.Request.Url.ToString();
            var urlParts = url.Split(new char[] { '/' });
    
            if (!urlParts.Contains("Console"))
            {
                var controllerLink = helper.ActionLink(
                    linkText: controllerName.SplitTitleWords(),
                    actionName: "Index",
                    controllerName: controllerName);
                sb.Append($"<li>{controllerLink}</li>");
            } else
            {
                var a = $"<a href="{url}">Console</a>";
                sb.Append($"<li>{a}</li>");
            }
    
            var actionName = helper.ViewContext.RouteData.Values["action"].ToString();
            sb.Append($"<li class="active">{actionName.SplitTitleWords()}</li>");
    
            result = sb.Append("</ol>").ToString();
        }
        return result;
    }
    

    拆分标题词是我编写的自定义扩展,用于将 AddPDFResource 等单词分解为 Add PDF Resource

    public static string SplitTitleWords(this string value)
    {
        var cList = new List<char>();
        if (!string.IsNullOrEmpty(value))
        {
            cList.Add(value[0]); // just add the first letter, whether caps, no caps, or number
            for (var i = 1; i < value.Length; i++)
            {
                var c = value[i];
                if (char.IsUpper(c))
                {   //                                01234567891234    0123456789012345  
                    // check special cases like class AddPDFResource => Add PDF Resource
                    var c0 = value[i - 1];
                    if (char.IsUpper(c0))
                    {
                        if (i + 1 < value.Length)
                        {
                            var c1 = value[i + 1];
                            if (!char.IsUpper(c1))
                            {
                                cList.Add(' ');
                            }
                        }
                    } else
                    {
                        cList.Add(' ');
                    }
                }
                cList.Add(c);
            }
        }
        var result = new String(cList.ToArray());
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-24
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多