asp.net generates static post redundant code removing the code generated by viewstate

  • 2020-05-17 05:16:18
  • OfStack

Looking at their own website compiled after the source file, there are a lot of messy 78 bad things, read it feel uncomfortable
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE3NjgxMzM0MDIPZBYCZg9kFgIC ...  

There is also the form source file, which I can tell is the asp.net page
<form name="form1" method="post" action="smartDropDownList.aspx" id="form1">

Knowing that this site is also made by ASP.NET, I was curious to look at the front page of the bloggarden source file.

I also found some information on the Internet;

Summed up 1 of my own ideas:
1. If there is no complex data interaction on one of your pages, for example, you can use ajax to log in, and the single news interface can be removed. If there is any message, you can use ajax to complete it!

2, background management page, data interaction 1 are a little bit complex, I think there is no need to remove what things, anyway, the background has no impact on SEO, I curiously looked at the blog garden background is also some
No more crap, just post the code and add the following function to your page code
 
protected override void Render(HtmlTextWriter writer) 
{ 
System.IO.StringWriter html = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html); 
base.Render(tw); 
string Temp = html.ToString(); 
string s1 = string.Empty, s2 = string.Empty, s3 = string.Empty, s4 = string.Empty, s5 = string.Empty, s6 = string.Empty; 
int i = 0; 
int j = 0; 
i = Temp.IndexOf("<form"); 
if (i > 0) 
{ 
j = Temp.IndexOf(">", i); 
s1 = Temp.Substring(0, i); 
s2 = Temp.Substring(j + 1, Temp.Length - j - 1); 
} 
i = s2.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\""); 
if (i > 0) 
{ 
j = s2.IndexOf(">", i); 
s3 = s2.Substring(0, i); 
s4 = s2.Substring(j + 1, s2.Length - j - 1); 
} 
i = s4.IndexOf("<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\""); 
if (i > 0) 
{ 
j = s4.IndexOf(">", i); 
s5 = s4.Substring(0, i); 
s6 = s4.Substring(j + 1, s4.Length - j - 1); 
Temp = s1 + s3 + s5 + s6; 
} 
else 
{ 
Temp = s1 + s3 + s4; 
} 
Temp = Temp.Replace("</form>", ""); 
Temp = Temp.Replace("\r\n", ""); 
tw.Close(); 
Response.Write(Temp); 
} 

In fact, I rewrote the Render method, and then I intercepted the html source file, so I can solve the problem.
If you feel simple, or disdain, do not spray, have a problem please clap brick, thank you!

2. The solution to query foreigners is to move the code to the end of the page, but find that some of the page error, and then found that the enableviewatate=false viewstate code significantly reduced.

Reason summary:
(1) this is because net records the view state of the control. asp.net server controls are recorded by default.
If you are sure that a control does not need to record control state, you can assign false to its EnableViewState to disable it.
Personal summary:
Closing the view is not the final solution, only a reduction. The most radical solution is to turn the form form into a normal html tag.
Leave out runat="server". Programs, important as they are, are meaningless if they are not effective.
Netizens' opinions:
View state, this is not some dumb code. You use a server control, why is the information read as a control object after every submission? It's all because of view state.

View state can be disabled, but only using EnableViewState=false is not complete. You will also see the hidden areas of s 55en on the page. This is because even if it is disabled, there will still be a server control used there, from runat="server". If you program from with a normal html tag, the page will be clean.

Of course, disabling view state makes the system more efficient, because the program no longer analyzes view state and initializes control objects. Doing so is efficient for a website, but it's a bit of a hassle when it comes to making referrals.

Related articles: