最近我开始使用 <%=
在我的 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/