已解决:URL带有查询字符串时,ASP生成的XML无效.怎么修? [英] SOLVED: ASP-generated XML not valid when URL has query string. How to fix?

查看:91
本文介绍了已解决:URL带有查询字符串时,ASP生成的XML无效.怎么修?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面被调用为Test.aspx?id=102,它执行以下操作:

I have a page that is invoked as Test.aspx?id=102 that does this:

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


方法GenerateData返回一个XmlDocument对象,该对象包含使用查询字符串参数id检索的数据.
我的问题是,如果URL有查询字符串,则浏览器会将返回的文档视为格式错误的HTML.如果我省略查询字符串并将硬编码常量传递给GenerateData,则返回的文档将被视为XML文档,并按照我的期望显示.

我已经尝试过Request.QueryString.ClearRequest.QueryString.Remove("id"),并且两者均生成集合为只读"错误.

我想保留通过查询字符串传递搜索条件的灵活性,而不必处理Session对象或表单提交.有关如何使其正常工作的任何建议?

添加

不管是什么,无论我使用查询字符串还是内联常量,这都是生成的XML:


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 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>


使用该常数,该常数将由浏览器(至少是IE 8)显示为格式正确的XML文件.使用查询字符串,将显示该文件,就好像它的HTML格式不正确一样.

解决方案

男孩,我觉得自己是个白痴吗?我在页面上添加了OutputCache Location="None"指令,突然问题消失了.显然,查询字符串版本已经缓存了一些非显而易见的错误,并且服务器正在返回该版本. :doh:

没关系,在这里什么也看不到.


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.

推荐答案

我认为这与查询字符串无关.也许当您拥有查询字符串时返回的XML是不同的,这就是为什么看到不同的原因?我尝试了以下示例:
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:
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


我在IE8和Firefox中使用和不使用查询字符串都进行了尝试,它始终看起来像XML.

仅供参考,您无法清除请求查询字符串的原因是,一旦请求到达服务器后就无法对其进行修改.到那时,已经发出了请求……这是您必须关注的响应.如果需要,可以将响应重定向到另一个URL,但这听起来不像您想要的.


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.


这篇关于已解决:URL带有查询字符串时,ASP生成的XML无效.怎么修?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆