Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示HTML代码,如&;注册或&;在Html.ActionLink文本中复制_Html_Asp.net Mvc_Html.actionlink - Fatal编程技术网

显示HTML代码,如&;注册或&;在Html.ActionLink文本中复制

显示HTML代码,如&;注册或&;在Html.ActionLink文本中复制,html,asp.net-mvc,html.actionlink,Html,Asp.net Mvc,Html.actionlink,不幸的是,我在这方面的研究没有成功。 有了锚定标签,我可以做到: <a href="..."> My Link &reg </a> 但输出与输入相同,而不是预期的reg符号。 有什么想法吗 提前谢谢 @Html.ActionLink("My Link ®", "Action") 或 ActionLink始终使用HttpUtility调用。对链接文本进行编码 您可以使用UrlHelper方法,如 <a href="@Url.Action("Action")

不幸的是,我在这方面的研究没有成功。 有了锚定标签,我可以做到:

<a href="..."> My Link &reg </a>
但输出与输入相同,而不是预期的reg符号。 有什么想法吗

提前谢谢

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


ActionLink始终使用HttpUtility调用。对链接文本进行编码

您可以使用UrlHelper方法,如

<a href="@Url.Action("Action")">My Link &reg</a>

您可以使用(
MvcHtmlString
in)来表示您不希望对其重新编码:

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

下面是我如何在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 ) );
}
//
///基于传入的控制器类型和方法创建定位标记。
///不编码传入的链接文本。
/// 
///控制器类型
///HTML助手
///路由到的方法
///要显示在页面上的链接文本
///格式化的锚定标记
公共静态MvcHtmlString操作链接(此HtmlHelper HtmlHelper,
表达行动,
HtmlString linkText),其中TController:Controller
{
返回ActionLink(htmlHelper,action,linkText,null,null);
}
/// 
///基于传入的控制器类型和方法创建定位标记。
///不编码传入的链接文本。
/// 
///控制器类型
///HTML助手
///路由到的方法
///要显示在页面上的链接文本
///路线值
///HTML属性
///格式化的锚定标记
公共静态MvcHtmlString操作链接(此HtmlHelper HtmlHelper,
表达行动,
HtmlString链接文本,
对象路由值,
对象htmlAttributes),其中TController:Controller
{
var routingValues=GetRouteValuesFroExpression(操作,routeValues);
var url=UrlHelper.GenerateUrl(null,//routeName
null,//actionName
null,//controllerName
路由值,
htmlHelper.RouteCollection,
htmlHelper.ViewContext.RequestContext,
false);//includeImplicitMVCvalue
var tagBuilder=新的tagBuilder(“a”)
{ 
InnerHtml=!String.IsNullOrEmpty(linkText.ToString())?linkText.ToString():String.Empty
};
tagBuilder.MergeAttributes((IDictionary)htmlAttributes);
tagBuilder.MergeAttribute(“href”,url);
返回MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
}
它是强类型的,就像在MVC futures NuGet包中一样。所以你可以这样使用它:

<%= Html.ActionLink<HomeController>( x => x.Index(),
                                     new HtmlString( "Don't Encode Me!<sup>&reg;</sup>" ) ) %>
x.Index(),
新HtmlString(“不要给我编码!®;”)%%>

看起来不错,但Html.ActionLink不接受HtmlString作为输入类型(必须是字符串,而不是HtmlString),该死,尽管创建一个重载的扩展方法并不困难。忽略我之前的评论。继续实施这项计划。请看我的回答:开始时,我的想法与您的第一个建议相同,但如果您在代码中看到特殊字符,这不是一种糟糕的代码样式吗?
/// <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 ) );
}
<%= Html.ActionLink<HomeController>( x => x.Index(),
                                     new HtmlString( "Don't Encode Me!<sup>&reg;</sup>" ) ) %>