0
好吧,我讨厌实施我不太明白的事情,而我正在为这个失去睡眠。这段代码为什么不重复页面的主体?
这里是情况:我正在开发一个ASP.NET网站,前一段时间由其他人开始开发,他们当时正在使用VB.NET。我被要求将我的网页的VIEWSTATE移动到页面的底部,因为这应该能够提高网页蜘蛛对我们网页的搜索能力。
因此,经过几个小时的搜索,我把这堂课拼凑在一起,到目前为止效果很好。但它让我感到困扰,我不明白它为什么运作良好,请帮我理解它为什么有效。下面是类代码:
Public Class Page
Inherits System.Web.UI.Page
Protected Overrides Sub Render(writer As System.Web.UI.HtmlTextWriter)
Dim viewStateStart As Integer
Dim viewStateEnd As Integer
Dim viewState As String = ""
Dim FormEnd As Integer
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
' WHY does this work? Why is this call to the base method not causing
' the HTML body to be duplicated????
MyBase.Render(hw)
Dim bodyHTML As String = sw.ToString()
hw.Close()
sw.Close()
viewStateStart = bodyHTML.IndexOf("<input type=""hidden"" name=""__VIEWSTATE""")
If viewStateStart > -1 Then
viewStateEnd = bodyHTML.IndexOf("/>", viewStateStart) + 2
viewState = bodyHTML.Substring(viewStateStart, viewStateEnd - viewStateStart)
bodyHTML = bodyHTML.Remove(viewStateStart, viewStateEnd - viewStateStart)
FormEnd = bodyHTML.IndexOf("</form>") - 1
bodyHTML = bodyHTML.Insert(FormEnd, viewState)
End If
writer.Write(bodyHTML)
End Sub
End Class
我的问题是,我不明白我的生活,怎么来的这个类调用基方法Render,并与不使所述ASP页面的主体两次逃脱,因为基本方法也会调用Writer.Write(htmlWriter)
为什么不是这种情况?
感谢您的任何帮助和意见人。
我想我现在可以睡了:)非常感谢Andrei。当我达到声望15我将能够upvote :) – 2014-12-05 15:21:05
@AjaxNgnurse,它的价值,你可以标记答案为接受。主要是为了表明它对你有帮助,以防其他人有类似的问题并找到这个线程 – Andrei 2014-12-05 18:11:34