asp.net-mvc 如何在 Asp.Net MVC 循环中呈现纯 HTML 链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1624697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:55:09  来源:igfitidea点击:

How to render plain HTML links in Asp.Net MVC loop?

asp.net-mvchyperlink

提问by Joannes Vermorel

I would like to render a list of HTML links in ASP.NET MVC. Note that the links are absolute and externalto the website being designed. The following code works:

我想在 ASP.NET MVC 中呈现 HTML 链接列表。请注意,链接是绝对和外部正在设计的网站。以下代码有效:

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.Encode(item.Id) %>
        </td>
        <td>
            <%= String.Format("<a href=\"{0}\">link</a>", item.Url) %>
        </td>
    </tr>

<% } %>

But I am wondering if it's really the right approach. Am I missing some obvious MVC control here?

但我想知道这是否真的是正确的方法。我在这里错过了一些明显的 MVC 控制吗?

采纳答案by Misha N.

You are not missing anything but good approach is to create extender method on HtmlHelper:

您没有遗漏任何东西,但好的方法是在 HtmlHelper 上创建扩展程序方法:

public static class HtmlHelpers
    {

        public static string SimpleLink(this HtmlHelper html, string url, string text)
        {
            return String.Format("<a href=\"{0}\">{1}</a>", url, text);
        }

    }

then you can use it like this:

那么你可以像这样使用它:

<tr>
        <td>
            <%= Html.Encode(item.Id) %>
        </td>
        <td>
            <%= Html.SimpleLink(item.Url,item.Text) %>
        </td>
    </tr>

[edit] I forgot to add. In order to use this HtmlHelper extender throughout application you need to add the following in the web config file:

[编辑] 我忘了补充。为了在整个应用程序中使用此 HtmlHelper 扩展程序,您需要在 Web 配置文件中添加以下内容:

<system.web>
      <pages>
         <namespaces>
            <!-- leave rest as-is -->
            <add namespace="theNamespaceWhereHtmlHelpersClassIs"/>
        </namespaces>
      </pages>
    </system.web>

回答by Glenn Slaven

I like to implement it the way the MVC framework does it, using the tag builder class. This way I can pass through the htmlAttributesparameter to add things like class or other attributes:

我喜欢用 MVC 框架的方式来实现它,使用标签构建器类。这样我就可以通过htmlAttributes参数来添加类或其他属性之类的东西:

public static MvcHtmlString HtmlLink(this HtmlHelper html, string url, string text, object htmlAttributes)
{
 TagBuilder tb = new TagBuilder("a");
 tb.InnerHtml = text;
 tb.MergeAttributes(new RouteValueDictionary(htmlAttributes));
 tb.MergeAttribute("href", url);
 return MvcHtmlString.Create(tb.ToString(TagRenderMode.Normal));
}

May seem like overkill just to generate a link, but it means you don't have to muck about with string format patterns to insert additional HTML attributes on the link

仅仅生成一个链接似乎有点矫枉过正,但这意味着您不必考虑字符串格式模式来在链接上插入额外的 HTML 属性

回答by Max

I would rather use

我宁愿使用

<td><a href="<%= item.Url %>">link</a></td>

seems somewhat "cleaner" to me, but I think your approach just as good.

对我来说似乎有些“更干净”,但我认为你的方法同样好。

回答by Edward Wilde

Orchard project has an HtmlHelper extensions class which has a link builder method.

Orchard 项目有一个 HtmlHelper 扩展类,它有一个链接构建器方法。

See: HtmlHelperExtensions.Link()

请参阅:HtmlHelperExtensions.Link()

http://orchard.codeplex.com/SourceControl/changeset/view/dbec3d05e6d1#src%2fOrchard%2fMvc%2fHtml%2fHtmlHelperExtensions.cs

http://orchard.codeplex.com/SourceControl/changeset/view/dbec3d05e6d1#src%2fOrchard%2fMvc%2fHtml%2fHtmlHelperExtensions.cs

Allows the following usage:

允许以下用法:

<li>@Html.Link(Model.Path, Model.Title)</li> 

UpdateThe above link is no longer valid, but if you download the source, you will find HtmlHelperExtensions which has 5 overloads for Links, one of which looks like this:

更新上面的链接不再有效,但是如果您下载源代码,您会发现 HtmlHelperExtensions 有 5 个链接重载,其中之一如下所示:

public static IHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary<string, object> htmlAttributes) {
        var tagBuilder = new TagBuilder("a") { InnerHtml = htmlHelper.Encode(linkContents) };
        tagBuilder.MergeAttributes(htmlAttributes);
        tagBuilder.MergeAttribute("href", href);
        return new HtmlString(tagBuilder.ToString(TagRenderMode.Normal));
    } 

回答by Mehdi Golchin

I think it's good. A simple foreach does the repeater role in MVC.

我认为这很好。一个简单的 foreach 在 MVC 中扮演中继器的角色。

回答by Pravin Patil

To avoid Html encoding use @Html.Raw(url).

为避免 Html 编码,请使用 @Html.Raw(url)。