停止标签制造商逃逸单引号ASP.NET MVC 2 [英] Stop the tag builder escaping single quotes ASP.NET MVC 2

查看:138
本文介绍了停止标签制造商逃逸单引号ASP.NET MVC 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想创建一个按钮,做一个重定向的JavaScript下面的HtmlHelper 方法:

I have the following HtmlHelper method that I want to create a button that does a redirect with JavaScript:

public static string JavaScriptButton(this HtmlHelper helper, string value,
                        string action, string controller, object routeValues = null, object htmlAttributes = null)
{
    var a = (new UrlHelper(helper.ViewContext.RequestContext))
                            .Action(action, controller, routeValues);

    var builder = new TagBuilder("input");
    builder.Attributes.Add("type", "submit");
    builder.Attributes.Add("value", value);
    builder.Attributes.Add("class", "button");
    builder.Attributes.Add("onclick", string.Format("javascript:location.href='{0}'", a));
    builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

    return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing)).ToString();         
}

问题是,创建该onclick处理该行正变得由tagbuilder逃脱,由此产生的HTML是:

The problem is that the line that creates the onclick handler is getting escaped by the tagbuilder, the resulting html is:

<input class="button" onclick="javascript:location.href=&#39;&#39;" type="submit" value="Return to All Audits" />

反正我能阻止这种行为?

Is there anyway I can stop this behaviour?

推荐答案

这实际上是.NET 4.0的问题。要解决它,你需要重写属性编码过程。

This is actually an issue with .NET 4.0. To fix it you need to override the attribute encoding process.

public class HtmlAttributeNoEncoding : System.Web.Util.HttpEncoder
{
    protected override void HtmlAttributeEncode(string value, System.IO.TextWriter output)
    {
        output.Write(value);
    }
}

然后把它放在你的web.config文件下的&LT;&的System.Web GT; 元素:

<httpRuntime encoderType="HtmlAttributeNoEncoding"/>

我发现这个这里

这篇关于停止标签制造商逃逸单引号ASP.NET MVC 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆