dag*_*da1 16 asp.net asp.net-mvc-2
我有以下HtmlHelper方法,我想创建一个使用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();
}
Run Code Online (Sandbox Code Playgroud)
问题是创建onclick处理程序的行被tagbuilder转义,生成的html是:
<input class="button" onclick="javascript:location.href=''" type="submit" value="Return to All Audits" />
Run Code Online (Sandbox Code Playgroud)
无论如何我可以阻止这种行为吗?
Rya*_*yan 14
这实际上是.NET 4.0的一个问题.要修复它,您需要覆盖属性编码过程.
public class HtmlAttributeNoEncoding : System.Web.Util.HttpEncoder
{
protected override void HtmlAttributeEncode(string value, System.IO.TextWriter output)
{
output.Write(value);
}
}
Run Code Online (Sandbox Code Playgroud)
然后将它放在<system.web>元素下的web.config文件中:
<httpRuntime encoderType="HtmlAttributeNoEncoding"/>
Run Code Online (Sandbox Code Playgroud)
我在这里找到了这个.
虽然Ryan提供的解决方案可能有效,但有点像使用锤子拍打苍蝇.
问题是TagBuilder在调用MergeAttributes()期间对字符串进行编码.对于按钮链接中所需的Javascript,这会影响单引号和空格.
所需扩展方法的最后一步是返回MvcHtmlString(不接收进一步的编码),因此在创建该对象之前对字符串进行一些简单的文本更正(以撤消编码)是完全合理的.
return new MvcHtmlString(tb.ToString(TagRenderMode.Normal).Replace("'", "\'").Replace(" "," "));
Run Code Online (Sandbox Code Playgroud)
public static class ActionLinkButtonHelper
{
public static MvcHtmlString ActionLinkButton(this HtmlHelper htmlHelper, string buttonText, string actionName, object routeValuesObject = null, object htmlAttributes = null)
{
return ActionLinkButton(htmlHelper, buttonText, actionName, "", routeValuesObject, htmlAttributes);
}
public static MvcHtmlString ActionLinkButton(this HtmlHelper htmlHelper, string buttonText, string actionName, string controllerName, object routeValuesObject = null, object htmlAttributes = null)
{
if (string.IsNullOrEmpty(controllerName))
{
controllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
}
RouteValueDictionary routeValues = new RouteValueDictionary(routeValuesObject);
RouteValueDictionary htmlAttr = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
TagBuilder tb = new TagBuilder("button");
tb.MergeAttributes(htmlAttr, false);
string href = UrlHelper.GenerateUrl("default", actionName, controllerName, routeValues, RouteTable.Routes, htmlHelper.ViewContext.RequestContext, false);
tb.MergeAttribute("type", "button");
tb.SetInnerText(buttonText);
tb.MergeAttribute("value", buttonText);
tb.MergeAttribute("onclick", "location.href=\'"+ href +"\';return false;");
return new MvcHtmlString(tb.ToString(TagRenderMode.Normal).Replace("'", "\'").Replace(" "," "));
}
}
Run Code Online (Sandbox Code Playgroud)
这将完成添加按钮链接所需的一切,具有您使用的最有用的重载,ActionLink并且不会通过更改属性编码过程而导致意外的应用程序范围更改.
| 归档时间: |
|
| 查看次数: |
7104 次 |
| 最近记录: |