my server seems to be sometimes returning wrong html to webclients
im using asp.net 4 with VS 2012. debugging on IIS Express.
in order to debug this issue, id like to trace the html that asp.net is sending
in the Global_asax_PreRequestHandlerExecute i can access the response code and status, but cant seem to find the body html
i tried to read the OutputStream like this:
Dim ms = New MemoryStream CurContext.Response.OutputStream.CopyTo(ms) Dim sr = New StreamReader(ms) Dim rtext = sr.ReadToEnd
but that throws a NotSupportedException Stream does not support reading.
any ideas?
thanks a lot
EDIT
i now tested this for sure
i have a label on the page with the following attributes
<asp:label id="l" runat="server" Font-Bold="true" Font-Size="X-Large" BackColor="Pink"/>
when displayed in the browser it shows just fine, as follows:
<span id="C1_FormView1_l" style="background-color:Pink;font-size:X-Large;font-weight:bold;">Processed</span>
but when downloaded with webclient i get
<span id="C1_FormView1_l"><b><font size="6">Processed</font></b></span>
why is the backcolor lost? and btw, why doesn’t it use the more modern style attribute instead of adding b and font
if i could read the ResponseStream i would at least know WHERE it gets lost, even that i dont know now.
thank you very much
P.S. if .net 4.5 is better for this, then i might consider changing the target framework
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can get the HTML (and modify it) by overriding Render on the page / site master.
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(ms))
{
HtmlTextWriter tw = new HtmlTextWriter(sw);
base.Render(tw);
tw.Flush();
ms.Position = 0;
using (System.IO.StreamReader sr = new System.IO.StreamReader(ms))
{
string yourHTML = sr.ReadToEnd();
// do stuff with yourHTML
Response.Write(yourHTML);
sr.Close();
tw.Dispose();
}
}
}
}
in vb
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Using ms As New System.IO.MemoryStream()
Using sw As New System.IO.StreamWriter(ms)
Dim tw As HtmlTextWriter = New HtmlTextWriter(sw)
MyBase.Render(tw)
tw.Flush()
ms.Position = 0
Using sr As New System.IO.StreamReader(ms)
Dim yourHTML As String = sr.ReadToEnd()
'do stuff with yourHTML'
Response.Write(yourHTML)
sr.Close()
tw.Dispose()
End Using
End Using
End Using
End Sub
Method 2
this does not answer my original question technically, but it does solve the issue i was having
the problem was that the html wasnt rendering correctly
i now remembered that aspx has adaptive rendering, so i fgured the useragent used in the request might be to blame
i changed my code to:
Dim myReq As HttpWebRequest = WebRequest.Create(MailUrl) myReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1" Dim resp As HttpWebResponse = myReq.GetResponse Dim stream = resp.GetResponseStream Dim rdr = New StreamReader(stream) Dim BodyText = rdr.ReadToEnd
and now the html is rendering in correct modern Html5/Css3 markup
i appreciate your help and guidance.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0