不幸的是,我对这个主题的研究没有成功。使用锚标签,我能够做到这一点:
<a href="..."> My Link ® </a>
现在我希望与 Html.Actionlink 相同:
@Html.ActionLink("My Link ®", "Action")
但是输出与输入相同,而不是预期的 reg 符号。任何想法?
提前致谢!
不幸的是,我对这个主题的研究没有成功。使用锚标签,我能够做到这一点:
<a href="..."> My Link ® </a>
现在我希望与 Html.Actionlink 相同:
@Html.ActionLink("My Link ®", "Action")
但是输出与输入相同,而不是预期的 reg 符号。任何想法?
提前致谢!
@Html.ActionLink("My Link ®", "Action")
或者
<a href="@Url.Action("Action")"> My Link ® </a>
这是我在 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>®</sup>" ) ) %>
ActionLink 总是对链接文本使用 HttpUtility.Encode 调用。
您可以使用 UrlHelper 方法,例如
<a href="@Url.Action("Action")">My Link ®</a>
您可以使用HtmlString
( MvcHtmlString
in .NET 2 / MVC 2 ) 表示您不希望它被重新编码:
@Html.ActionLink(new HtmlString("My Link ®"), "Action");