Spostare il tag __VIEWSTATE alla fine della pagina ASP.NET

di Massimo Cappellesso, in UserScript, ASP.NET, Web Form,

Chi sviluppa con ASP.NET avrà sicuramente a che fare con il ViewState, che rappresenta lo stato della pagina dopo la sua ultima elaborazione sul server ed è utilizzato per mantenere lo stato dei controls di una pagina.
Vi sarete anche accorti che nel caso i controlli siano molti il campo hidden __VIEWSTATE si allunga a dismisura, spostando in basso il codice HTML della pagina, cosa che ai motori di ricerca non piace più di tanto.
Con questo script, utlizzato anche da DotNetNuke, vedremo come spostare il tag input alla fine del form, creando una classe base che estende la System.Web.UI.Page.
Una volta creato l'assembly basterà ereditare la classe base nella pagina con la direttiva Inherits.

Public Class BasePage
  Inherits System.Web.UI.Page

  Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
    Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
    MyBase.Render(htmlWriter)
    Dim html As String = stringWriter.ToString()
    Dim StartPoint As Integer = html.IndexOf("<input type=""hidden"" name=""__VIEWSTATE""")
    If StartPoint >= 0 Then
      Dim EndPoint As Integer = html.IndexOf("/>", StartPoint) + 2
      Dim ViewStateInput As String = html.Substring(StartPoint, EndPoint - StartPoint)
      html = html.Remove(StartPoint, EndPoint - StartPoint)
      Dim FormEndStart As Integer = html.IndexOf("</form>") - 1
      If FormEndStart >= 0 Then
        html = html.Insert(FormEndStart, ViewStateInput)
      End If
    End If
    writer.Write(html)
  End Sub
End Class

Commenti

Visualizza/aggiungi commenti

| Condividi su: Twitter, Facebook, LinkedIn

Per inserire un commento, devi avere un account.

Fai il login e torna a questa pagina, oppure registrati alla nostra community.

Approfondimenti

I più letti di oggi