获取 Html.ActionLink 生成多行链接

发布于 2024-11-27 01:29:14 字数 307 浏览 0 评论 0原文

对于我正在开发的应用程序,我需要让图像标题跨越多行。玩弄一下,我能够从此标记中获得所需的效果:

<a href="http://www.stackoverflow.com">Stack<br>Overflow</a>

为我渲染了这个:

Stack
Overflow

但是我一直无法找到为 HTML.ActionLink 生成链接描述的正确方法,该链接描述将呈现此类输出。

For an application i am working on, I need to have the image captions span multiple lines. Playing around I was able to get the desired effect from this markup:

<a href="http://www.stackoverflow.com">Stack<br>Overflow</a>

rendered this for me:

Stack
Overflow

However I have been unable to find the right way to generate a link description for HTML.ActionLink that will render this kind of output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁年轻赶紧闹 2024-12-04 01:29:14

如果 url 不是您网站的一部分,那么使用 HTML 帮助程序生成此链接不会带来太多价值。相反,只需按原样编写即可。如果它是您网站的一部分,并且您可以使用路由来到达地址,您可以编写一个自定义 HTML 帮助程序,默认情况下它不会对链接文本进行 HTML 编码:

public static class HtmlExtensions
{
    public static MvcHtmlString MyActonLink(
        this HtmlHelper html, 
        string linkText, 
        string action, 
        string controller, 
        object routeValues, 
        object htmlAttributes
    )
    {
        var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
        var url = urlHelper.Action(action, controller, routeValues);
        var anchor = new TagBuilder("a");
        anchor.InnerHtml = linkText;
        anchor.Attributes["href"] = url;
        anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        return MvcHtmlString.Create(anchor.ToString());
    }
}

在您看来:

@Html.MyActonLink(
    "Stack<br/>Overflow",    // linkText
    "MyAction",              // actionName
    "MyController",          // controllerName
    new { id = "123" },      // routeValues
    new { @class = "foo" }   // htmlAttributes
)

If the urls is not part of your web site using an HTML helper to generate this link wouldn't bring much value. Instead simply write it as is. And if it is part of your site and you could use routing to reach the address you could write a custom HTML helper which doesn't HTML encode the link text by default:

public static class HtmlExtensions
{
    public static MvcHtmlString MyActonLink(
        this HtmlHelper html, 
        string linkText, 
        string action, 
        string controller, 
        object routeValues, 
        object htmlAttributes
    )
    {
        var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
        var url = urlHelper.Action(action, controller, routeValues);
        var anchor = new TagBuilder("a");
        anchor.InnerHtml = linkText;
        anchor.Attributes["href"] = url;
        anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        return MvcHtmlString.Create(anchor.ToString());
    }
}

and in your view:

@Html.MyActonLink(
    "Stack<br/>Overflow",    // linkText
    "MyAction",              // actionName
    "MyController",          // controllerName
    new { id = "123" },      // routeValues
    new { @class = "foo" }   // htmlAttributes
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文