Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a page that is invoked as Test.aspx?id=102 that does this:

VB
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
    Dim ThisId As String = Request.QueryString("id")

    Dim Doc As XmlDocument = GenerateData(ThisId)

    Response.Buffer = True
    Response.ClearContent()
    Response.ClearHeaders()
    Response.ContentType = "text/xml"
    Response.Write(Doc.OuterXml)
    Response.End()
End Sub

The method GenerateData returns an XmlDocument object containing data retrieved using the query string parameter id.

My problem is that if the URL has a query string, the returned document is treated by the browser as ill-formatted HTML. If I omit the query string and pass a hard coded constant into GenerateData, the returned document is treated like an XML document and displays exactly as I would expect.

I have tried Request.QueryString.Clear and Request.QueryString.Remove("id"), and both generate a "Collection is read-only" error.

I would like to retain the flexibility of passing the search criteria through the query string and not have to deal with the Session object or form submissions. Any suggestions on how to get this to work?

ADDED

For what it is worth, this is the XML that gets generated whether I use the query string or an inline constant:
XML
<?xml version="1.0" ?>
<EBulletinData>
  <Item>
    <EBulletinDate>6/9/2010 12:00:00 AM</EBulletinDate>
    <DepartmentId>5</DepartmentId>
    <Id>102</Id>
    <Introduction>
      <![CDATA[<p>Some text here.</p>]]>
    </Introduction>
    <Title>
      <![CDATA[Client Quarterly Available]]>
    </Title>
  </Item>
</EBulletinData>

With the constant, this is displayed by the browser (IE 8, at least) as a well-formatted XML file. With the query string, this same file is displayed as if it were badly-formatted HTML.

Solution

Boy, do I feel like a right idiot. I added the OutputCache Location="None" directive to the page, and suddenly the problem went away. Apparently, the query string version had been cached with some non-obvious errors, and it was this version that the server was returning. :doh:

Never mind, nothing to see here.
Posted
Updated 28-Dec-10 10:09am
v3
Comments
AspDotNetDev 28-Dec-10 16:35pm    
I gave you a 5 for having a clear question in the first place and then coming back to explain the problem/solution after you figured it out.

1 solution

I do not think this has to do with the query string. Perhaps the XML you return when you have a query string is different, and that is why you are seeing a difference? I tried this example:
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
	Response.Buffer = True
	Response.ClearContent()
	Response.ClearHeaders()
	Response.ContentType = "text/xml"
	Response.Write("<test>Hello</test>")
	Response.End()
End Sub

I tried it in IE8 and Firefox with and without a query string and it always looked like XML.

FYI, the reason you cannot clear the request query string is because you cannot modify the request once it has reached the server. At that point, the request has been made... it is the response you must concern yourself with. If you wanted, you could redirect the response to a different URL, but that doesn't sound like what you want here.
 
Share this answer
 
Comments
Gregory Gadow 28-Dec-10 15:58pm    
Nope. With or without the query string, I am passing the exact same same value into GenerateData, and the exact same document is being passed back out. The ONLY difference between using and not using the query string is the existence of the query string itself.
AspDotNetDev 28-Dec-10 16:03pm    
Hmmm, well my example works fine for me. Can try mine and see if it works for you?
Gregory Gadow 28-Dec-10 16:10pm    
Problem solved with the ingestion of extra caffeine and the use of an OutputCache directive. Thanks anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900