2

不幸的是,我对这个主题的研究没有成功。使用锚标签,我能够做到这一点:

<a href="..."> My Link &reg </a>

现在我希望与 Html.Actionlink 相同:

@Html.ActionLink("My Link &reg", "Action")

但是输出与输入相同,而不是预期的 reg 符号。任何想法?

提前致谢!

4

4 回答 4

2
@Html.ActionLink("My Link ®", "Action")

或者

<a href="@Url.Action("Action")"> My Link &reg </a>
于 2012-07-16T09:30:15.293 回答
1

这是我在 MVC 2 中解决这个问题的方法:

/// <summary>
/// Creates an anchor tag based on the passed in controller type and method.
/// Does NOT encode passed in link text.
/// </summary>
/// <typeparam name="TController">The controller type</typeparam>
/// <param name="htmlHelper">The HTML helper</param>
/// <param name="action">The method to route to</param>
/// <param name="linkText">The linked text to appear on the page</param>
/// <returns>A formatted anchor tag</returns>
public static MvcHtmlString ActionLink<TController>( this HtmlHelper htmlHelper,
                                                     Expression<Action<TController>> action,
                                                     HtmlString linkText ) where TController : Controller
{
    return ActionLink( htmlHelper, action, linkText, null, null );
}

/// <summary>
/// Creates an anchor tag based on the passed in controller type and method.
/// Does NOT encode passed in link text.
/// </summary>
/// <typeparam name="TController">The controller type</typeparam>
/// <param name="htmlHelper">The HTML helper</param>
/// <param name="action">The method to route to</param>
/// <param name="linkText">The linked text to appear on the page</param>
/// <param name="routeValues">The route values</param>
/// <param name="htmlAttributes">The HTML attributes</param>
/// <returns>A formatted anchor tag</returns>
public static MvcHtmlString ActionLink<TController>( this HtmlHelper htmlHelper,
                                                     Expression<Action<TController>> action,
                                                     HtmlString linkText,
                                                     object routeValues,
                                                     object htmlAttributes ) where TController : Controller
{
    var routingValues = GetRouteValuesFromExpression( action, routeValues );

    var url = UrlHelper.GenerateUrl( null, //routeName
                                     null, //actionName
                                     null, //controllerName
                                     routingValues,
                                     htmlHelper.RouteCollection,
                                     htmlHelper.ViewContext.RequestContext,
                                     false ); //includeImplicitMvcValues

    var tagBuilder = new TagBuilder("a")
        { 
            InnerHtml = !String.IsNullOrEmpty( linkText.ToString() ) ? linkText.ToString() : String.Empty
        };

    tagBuilder.MergeAttributes( (IDictionary<string, object>)htmlAttributes );
    tagBuilder.MergeAttribute( "href", url );

    return MvcHtmlString.Create( tagBuilder.ToString( TagRenderMode.Normal ) );
}

它是强类型的,就像在 MVC 期货 NuGet 包中一样。所以你可以像这样使用它:

<%= Html.ActionLink<HomeController>( x => x.Index(),
                                     new HtmlString( "Don't Encode Me!<sup>&reg;</sup>" ) ) %>
于 2013-01-23T20:12:13.210 回答
1

ActionLink 总是对链接文本使用 HttpUtility.Encode 调用。

您可以使用 UrlHelper 方法,例如

<a href="@Url.Action("Action")">My Link &reg</a>
于 2012-07-16T09:31:10.513 回答
1

您可以使用HtmlString( MvcHtmlStringin .NET 2 / MVC 2 ) 表示您不希望它被重新编码:

@Html.ActionLink(new HtmlString("My Link &reg"), "Action");
于 2012-07-16T09:59:49.610 回答