完全删除特定页面的 ViewState [英] Completely remove ViewState for specific pages

查看:26
本文介绍了完全删除特定页面的 ViewState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中包含一些不需要任何回发功能的页面.它们只是显示静态 HTML,甚至没有任何关联的代码.但是,由于母版页有一个 <form runat="server"> 标记,它包装了所有 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 ContentPlaceHolders, 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" %>

我意识到,当解密时,input 字段的这个值对应于

标记,我无法删除它,因为它在我的母版页上.但是,我仍然想删除仅显示静态 HTML 的页面的 ViewState 字段.可能吗?

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?

推荐答案

您可以覆盖 Render 并使用 Regex 将其剥离.

You could override Render and strip it out with a Regex.

按要求取样.(注意:这样做的开销几乎肯定会大于任何可能的好处!)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆