viewstate_将ViewState移到页面底部

viewstate

viewstate

I was working on some ASP.NET hacks and wanted to move the ViewState to the bottom of the page in order to get Google to pay more attention to my page and less to the wad of Base64'ed ViewState.

我当时正在处理一些ASP.NET黑客,想将ViewState移到页面底部,以便让Google对我的页面更多的关注,而不是对Base64的ViewState的关注。

First I tried this because it's the closest to the way my mind works:

首先,我尝试了此操作,因为它与我的思维方式最接近:

static readonly Regex viewStateRegex = new Regex(@"(<input type=""hidden"" name=""__VIEWSTATE"" 
value=""[a-zA-Z0-9\+=\\/]+"" />)",
RegexOptions.Multiline|RegexOptions.Compiled);
static readonly Regex endFormRegex = new Regex(@"</form>",
RegexOptions.Multiline|RegexOptions.Compiled);
 
protected override void Render(HtmlTextWriter writer)
{
    //Defensive coding checks removed for speed and simplicity. 
    // If these don't work out, you've likely got bigger problems.
    System.IO.StringWriter stringWriter = new System.IO.StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    base.Render(htmlWriter);
    string html = stringWriter.ToString();
    Match viewStateMatch = viewStateRegex.Match(html);
    string viewStateString = viewStateMatch.Captures[0].Value;
    html = html.Remove(viewStateMatch.Index,viewStateMatch.Length);
    // This will only work if you have only one </form> on the page
    Match endFormMatch = endFormRegex.Match(html,viewStateMatch.Index);
    html = html.Insert(endFormMatch.Index,viewStateString);
    writer.Write(html);
}

However, it was taking 1 thousanth of a second (~0.001230s) to do the work and that didn't feel right. Of course, by taking over the HtmlTextWriter and spitting it out as a string I've boogered up all the benefits of buffering and the whole streaming thing, but it still felt wrong.

但是,这需要花费千分之一秒(约0.001230秒)的时间来完成工作,并且感觉不对。 当然,通过接管HtmlTextWriter并将其作为一个字符串吐出来,我已经夸大了缓冲和整个流处理的所有好处,但是仍然感到不对。

So, against my better judgement, I did it again like this:

因此,根据我的更好判断,我再次这样做:

protected override void Render(System.Web.UI.HtmlTextWriter writer) 
{
    System.IO.StringWriter stringWriter = new System.IO.StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    base.Render(htmlWriter);
    string html = stringWriter.ToString();
    int StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
    if (StartPoint >= 0) 
    {
        int EndPoint = html.IndexOf("/>", StartPoint) + 2;
        string viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint);
        html = html.Remove(StartPoint, EndPoint - StartPoint);
        int FormEndStart = html.IndexOf("</form>") - 1;
        if (FormEndStart >= 0) 
        {
            html = html.Insert(FormEndStart, viewstateInput);
        }
    }
    writer.Write(html);
}

I always assumed (mistake #1) that IndexOf was pretty expensive, particularly on larger strings. However, this method averaged out at 0.000995s. It consistently beat the Regex one, even though the Regex one was very simple, the Regexes were precompiled and (I think) simple.

我一直认为(错误1)IndexOf相当昂贵,尤其是在较大的字符串上。 但是,此方法平均为0.000995s。 即使Regex非常简单,Regexe还是经过预编译且(我认为)简单,它始终击败Regex 1。

Now, to be clear, I'm just playing here, and I know it's microperf and premature optimization. The really interesting thing would be to do a matrix of page size vs. viewstate size. You know, large page, small viewstate against small page, large viewstate and all points in between, then try it with both techniques and see which is better for these different scenarios. But, I'm tired and have other things to do, so if you like, there's some homework for you. What does this data set look like: viewstate size vs. page size vs. technique?

现在,要明确地说,我只是在这里玩,我知道这是microperf和过早的优化。 真正有趣的是做一个页面大小与视图状态大小的矩阵。 您知道,大页面,小视图状态与小页面,大视图状态以及介于两者之间的所有点,然后尝试使用两种技术,看看哪种方法更适合这些不同的场景。 但是,我很累并且还有其他事情要做,因此,如果您愿意,可以做一些家庭作业。 这个数据集是什么样的:viewstate大小vs.页面大小vs.技术?

翻译自: https://www.hanselman.com/blog/moving-viewstate-to-the-bottom-of-the-page

viewstate

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值