Escape Razor输出(更改HtmlHelpers Textwriter)

时间:2016-02-17 08:48:59

标签: c# razor html-helper

我想创建一个Html-Helper,它使用Html,就像Razor的“原生”HtmlHelper一样。除了它应该通过用"替换\"来逃避所有输出。

的HtmlHelper码:

public static OtherContext ChangeContext(this HtmlHelper htmlHelper){
      return new OtherContext(htmlHelper);
}

OtherContext码:

public class OtherContext{

    private HtmlHelper _escapingHtmlHelper;

    public OtherContext(HtmlHelper){
         ViewContext viewContext = new ViewContext(...);
         _escapingHtmlHelper = new HtmlHelper(viewContext, ..., new RouteCollection());
    }

    public HtmlHelper Html{
         get{ return _escapingHtmlHelper;}
    }

}

查看码:

using (var otherContext = Html.ChangeContext()) {
     otherContext.Html.TextBox(...)
}

使用otherContext.Html呈现的输出都应该被转义。

预期输出=>替换('“','\”'):

<input type=\"text\" value=\"\">

我尝试实现自定义WebViewPage和自定义TextWriter,我用它创建了Html-Helper。但两者都被忽略了。

有没有办法实现这个目标?

0 个答案:

没有答案