2

unfortunately, I was not successful with my research for this topic. With an anchor tag, I was able to do this:

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

Now I want the same with an Html.Actionlink:

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

But the output is the same as the input and not a reg symbol as it is intended. Any idea?

Thanks in advance!

LFish
  • 299
  • 1
  • 6
  • 16

4 Answers4

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

or

<a href="@Url.Action("Action")"> My Link &reg </a>
ldp
  • 360
  • 2
  • 11
  • At the beginning I thought the same as your first suggestion but ain't it a bad code style if you see special characters in your code? – LFish Jul 19 '12 at 06:54
1

ActionLink always use call of HttpUtility.Encode for the link text.

You can use UrlHelper Method like

<a href="@Url.Action("Action")">My Link &reg</a>
Yograj Gupta
  • 9,811
  • 3
  • 29
  • 48
1

You can use an HtmlString (MvcHtmlString in .NET 2 / MVC 2) to indicate that you do not wish it to be re-encoded:

@Html.ActionLink(new HtmlString("My Link &reg"), "Action");
Community
  • 1
  • 1
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
1

Here is how I solved this in 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 ) );
}

It is strongly typed, as in the MVC futures NuGet package. So you can use it like this:

<%= Html.ActionLink<HomeController>( x => x.Index(),
                                     new HtmlString( "Don't Encode Me!<sup>&reg;</sup>" ) ) %>
Big McLargeHuge
  • 14,841
  • 10
  • 80
  • 108