彻底删除的ViewState的特定页面 [英] Completely remove ViewState for specific pages
问题描述
我有一个网站,具有不需要任何回后功能的一些页面。他们只是显示的静态的HTML和甚至没有任何关联code。然而,由于母版页有一个<形式=服务器>
标签,包装均的ContentPlaceHolder
取值,由此产生的HTML总是包含在的ViewState 字段,例如:
I have a site that features some pages which do not require any post-back functionality. They simply display static HTML and don't even have any associated code. However, since the Master Page has a <form runat="server">
tag which wraps all ContentPlaceHolder
s, the resulting HTML always contains the ViewState field, i.e:
<input
type="hidden"
id="__VIEWSTATE"
value="/wEPDwUKMjEwNDQyMTMxM2Rk0XhpfvawD3g+fsmZqmeRoPnb9kI="
/>
编辑:我试着设置的的EnableViewState
在页面级别变种都没有运气可言:
I tried both variants of setting EnableViewState
on page level with no luck at all:
<%@ Page Language="C#" EnableViewState="false" %>
<%@ Page Language="C#" EnableViewState="true" %>
我意识到,那解密后的输入该值
字段对应&LT;形式&GT;
标签我不能删除,因为它是我的母版页上。不过,我还是想删除ViewState字段为只能显示静态的HTML页面。这可能吗?
I realize, that when decrypted, this value of the input
field corresponds to the <form>
tag which I cannot remove because it is on my master page. However, I would still like to remove the ViewState field for pages that only display static HTML. Is it possible?
推荐答案
您可以覆盖渲染,并用正则表达式剥离出来。
You could override Render and strip it out with a Regex.
的要求采样。 (NB:这样做的开销几乎肯定会超过任何可能的益处大于虽然)
Sample as requested. (NB: Overhead of doing this would almost certainly be greater than any possible benefit though!)
protected override void Render(HtmlTextWriter output)
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
base.Render(textWriter);
textWriter.Close();
string strOutput = stringWriter.GetStringBuilder().ToString();
strOutput = Regex.Replace(strOutput, "<input[^>]*id=\"__VIEWSTATE\"[^>]*>", "", RegexOptions.Singleline);
output.Write(strOutput);
}
这篇关于彻底删除的ViewState的特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!