The VIEWSTATE view State garbled characters are removed from the ASP. NET page. asp. netviewstate

Source: Internet
Author: User

The VIEWSTATE view State garbled characters are removed from the ASP. NET page. asp. netviewstate

 

Save all view status information and control status information on the page.

In the early stages of the project, the author encountered such a requirement: SEO-based development, when there is no access to the MVC FrameworkRazorSo you can only use the ASP. NET engine. If you use the server-side control of the ASP. NET engine, the ASP. NET page will generate_ Viewstate hidden field,__ Viewstate hides the"Garbled"It is extremely unfriendly to web crawlers !!
"Garbled:

<Div class = "aspNetHidden"> <input type = "hidden" name = "_ EVENTVALIDATION" id = "_ EVENTVALIDATION" value = "/accept + r/E5r2IBU8/callback + dimensions = "/> </div>View Code

AllProblemNow, inCommercial projectsDuring development, we need to remove the "garbled characters" generated by the _ Viewstate hidden domain.

Here we will discuss the following solutions.
Solution 1:
Remove the form tagRunat = "server"If the attribute is used, the _ viewstate hidden field can be generated, but the server control cannot be used. Therefore, this method does not work when you need to use the server correct control.
Solution 2:
Set the EnableViewState attribute of the server correction control to false. In this case, we can see that the Viewstate of the webpage is obviously reduced, but the view State still exists...
Solution 3:
Some solutions require some basic classes of. NET, suchSystem. Web. dll.
System. Web. dll encapsulatedLosFormatterClass.
InMSDNThis class is described as serialization of the view status of Web forms pages.
In this case, we need the Serialize method encapsulated in the LosFormatter class. The function is described as follows: Convert the objects in the limited object serialization (LOS) format into view Status values and place the results in System. IO. stream object.

The code for removing country garbled characters is as follows:

/// <Summary> /// the code for removing VIEWSTATE garbled characters is as follows: // </summary> protected LosFormatter losFormatter = new LosFormatter (); /// <summary> /// serialize the status information of all views and controls. /// </Summary> /// <param name = "viewState"> the Object in which the view State information is stored </param> protected override void SavePageStateToPersistenceMedium (object viewState) {string val = Request. url + "_ VIEWSTATE"; System. IO. memoryStream stream = new System. IO. memoryStream (); losFormatter. serialize (stream, viewState); stream. flush (); Session [val] = stream ;}View Code

 


View status viewstate of the aspnet Server Control

The first thing you need to understand is that when you press a Button like this to send the page to the server again, the server processes your page as follows: it will reinitialize the page you requested for the first time, that is, although you have done many other operations on the page after the first request, for example, this page allows users to change the text font at will. After you press a tag to change the text font, you can press the Button to send the page back to the server, let it process your Button event. At this time, the server reinitializes your page, which is only the status of your first request (that is, the text font state is not changed ), but why does the server handle your Button event? After sending the webpage back to you, the font of the article is indeed your Custom font? Didn't it just return to the status of your first request when initializing the page? This is the viewstate function. After the server initializes the page, it reads the viewstate hidden in the page of your sending back, reads the corresponding information, and changes the article font to your Custom font on the server side. In your example, when text is input in TextBox and sent to the server, the server can also obtain your input. The only difference is that if you disable view status, when the page is sent to you again, your TextBox is empty. If it is not disabled, when the page is sent back to you again, the TextBox still retains your last input value.

How to remove page VIEWSTATE and ASPNET

This is because you have used the server Control of asp.net. If you can avoid this, we recommend that you use the html control and runnat = server to reduce the "useless value" generated"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.