c# - 在 WebControl 中使用 `<%=` 是个坏主意吗

标签 c# asp.net declarative

最近我开始使用 <%=在我的 Web 控件中更常见。通常我会在代码隐藏中设置字符串属性,然后将它们输出到表单中。

这是个坏主意吗?

例如。

代码隐藏:

属性:

public string TheTitle { get; set; }
public string TheBody { get; set; }
public ContentItem TheContent { get; set; }
public string ContentId { 
  get 
    { return "content" + (TheContent != null) ? TheContent.Id.ToSTring() : "0"; }
}

页面加载:

TheTitle = TheContentItem.Title;
TheBody = TheContentItem.Body;

在页面上:

<div id='<%= ContentID %>'>    

  <h2 class='title'><%= TheTitle ?? "No Title" %></h2>
  <p><%= TheBody %></p>

</div>

最佳答案

只有当数据未经验证时才会出现问题。

使用 .NET 4 的 <%: TheBody %>语法是对潜在不可信数据进行编码的有效方法。在早期版本的框架中,您可以使用 <%= HttpUtility.HtmlEncode(TheBody) %>达到同样的效果。

关于c# - 在 WebControl 中使用 `<%=` 是个坏主意吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3662839/

相关文章:

asp.net - 在插入时获取下一个数字,在新的一年重置为 0

c# - 如何在声明本身中初始化泛型?

asp.net - 使用 jQuery 获取 ASP.Net Gridview 的 rowIndex

xul - 用户界面标记语言的优缺点

c# - Visual studio 2010 Bing map 按经度和纬度查找位置

c# - 如何对子类隐藏父属性

c# - 给定类型 ExpressionType.MemberAccess,如何获取字段值?

c# - 如何展平数组数组?

c# - 基于属性的编程有什么优点?

recursion - 在 Prolog 递归中正确使用 is/2 谓词