开发者

What is all this stuff in ASP.NET HTML output?

Please see this:

VIEWSTATE" id="_VIEWSTATE" value="/wEPDwUKMTMzMTM1NTAzNA9kFgJmD2QWAgIDD2QWJgIDDw8WBB4LTmF2aWdhdGVVcmwFKGh0dHA6Ly93d3cubGVhcm5kZXZub3cuY29tL1Byb2R1Y3RzLmFzcHgeBFRleHQFCVN1YnNjcmliZWRkAgUPDxYCHwAFHWh0dHA6Ly9vbmxpbmUubGVhcm5kZXZub3cuY29tZGQCBw8PFgIfAAUnaHR0cDovL3d3dy5sZWFybmRldm5vdy5jb20vRGVmYXVsdC5hc3B4ZGQCCQ8PFgIfAAUoaHR0cDovL3d3dy5sZWFybmRldm5vdy5jb20vUHJvZHVjdHMuYXNweGRkAgsPDxYCHwAFJ2h0dHA6Ly93d3cubGVhcm5kZXZub3cuY29tL1NhbXBsZXMuYXNweGRkAg0PDxYCHwAFKWh0dHA6Ly93d3cubGVhcm5kZXZub3cuY29tL3N1YnNjcmliZS5hc3B4ZGQCDw8PFgIfAAUjaHR0cDovL3d3dy5sZWFybmRldm5vdy5jb20vRkFRLmFzcHhkZAIRDw8开发者_StackOverflow中文版WAh8ABSxodHRwOi8v........ (it's 10 times bigger but I've removed it)

Is this HTML that ASP.NET produce? And does ASP.NET MVC do the same as well?


That is called ViewState.

ASP.NET MVC does not produce ViewState.


ASP.Net MVC does not produce viewstate, but that doesn't necessarily mean it's faster.

First of all, if you have that much viewstate in an ASP.Net webforms page you're probably doing something wrong. It's not that hard to avoid large viewstates in webforms.

Secondly, the things webforms puts into viewstate mostly substitutes for things mvc pages need to rebuild on each request. This tends to work in mvc's favor on public internet sites, where the bandwidth is both more expensive and slower than using additional server resources. For the private intranet on your company's LAN, it tends to favor webforms where upstream bandwidth from browser to web server is free, plentiful, and fast.


Web forms uses viewstate to maintain control state when you post back to the server. For instance, say you have a text box and a button on a web form. You type something in the text box and click the button. The button performs a post back to the server. When the page reloads, unless you cleared the text box yourself in server-side code, the text box would still contain whatever typed before you clicked the button.

What actually happened was that the data in the text box was stored in the hidden viewstate field and loaded into the text box again upon the page refreshing. The viewstate looks like a jumbled mess because it is base64 encoded. You can store things in the viewstate yourself manually also. In the page_load event of your page just do this:

this.ViewState["someVariable"] = "some value";

Now, if you did this on a blank web form, when you load the page you will see a viewstate field in the html like the one you pasted (except a lot smaller). If you copied the value from that hidden field and pasted it into a viewstate decoder (like this one) you would see someVariable with a value of "some value".

Now put a button on the form and in the button's click even do this:

Response.Write(this.ViewState["someVariable"].ToString());

You will see "some value" written out on the page. What you did was manually store something in the viewstate, it got rendered to the page in that hidden field, then that hidden field got posted back to the server and you read from it to display the data.

This is the primary functionality of web forms. MVC does not use viewstate. MVC relies on another technique known as model binding which would be out of the scope of your question. Google "asp.net mvc model binding" for more information on the way mvc maintains information across posts.

Slightly off-topic example of a viewstate joke:

I have a rather large community site called U413. I recently redesigned it to use ASP.net MVC 2.0. It's a community for programmers and they razz me all the time for using webforms. Well now that it doesn't use webforms I decided to play a little joke on them. MVC does not use viewstate but I went into a webform and generated a viewstate that contained some data and manually pasted the hidden field into my view in my MVC app.

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjkPFgIeE1U0MTNTZWNyZXRBY2Nlc3NLZXkFM2h0dHA6Ly93d3cudTQxMy5jb20vQ29udGVudC9VNDEzU2VjcmV0QWNjZXNzS2V5LmpwZ2RkpshYUvbSiUuE0YaYNBH0rvTGj4qEcquSqQeUs9ZpuIQ=" />

Already several people have gone snooping through the html source hoping to find little secrets or exploits and encountered this. They immediately decoded it and got a good laugh. Copy the value and paste it into the decoder I linked to earlier if you want to see.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜