在 C# 中为 jQuery 创建 XML
我正在尝试为 jQuery.get (AJAX) 调用生成一些 XML,但我从 C# 页面收到以下错误: “使用主题 css 文件需要页面上的标头控件。(例如 < code>)."
生成 XML 的文件是一个简单的 .aspx 文件,完全由以下部分组成:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChangePeopleService.aspx.cs" Inherits="ChangeRegister.Person.ChangePeopleService" EnableTheming="false" %>
使用 Linq-to-XML 的代码隐藏,其中工作正常:
XElement xml = new XElement("People",
from p in People
select new XElement("Person", new XAttribute("Id", p.Id),
new XElement("FirstName", p.FirstName)));
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Write(xml.ToString());
我知道该错误与 Web.Config 的
标记有关,因为当我删除 'styleSheetTheme' 和 'theme' 时' 属性,XML 就可以正常生成了。那么问题显然是所有其他页面都会失去其样式。所有这些让我认为我的做法是错误的。
我的问题是:在 C# 中生成 XML 供 jQuery AJAX 调用使用的公认方法是什么?
I'm trying to generate some XML for a jQuery.get (AJAX) call, and I'm getting the following error from my C# page: "Using themed css files requires a header control on the page. (e.g. <head runat="server" />
)."
The file generating the XML is a simple .aspx file, consisting entirely of:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChangePeopleService.aspx.cs" Inherits="ChangeRegister.Person.ChangePeopleService" EnableTheming="false" %>
with codebehind using Linq-to-XML, which is working ok:
XElement xml = new XElement("People",
from p in People
select new XElement("Person", new XAttribute("Id", p.Id),
new XElement("FirstName", p.FirstName)));
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Write(xml.ToString());
I know that the error relates to the Web.Config's <pages styleSheetTheme="default" theme="default">
tag, because when I remove the 'styleSheetTheme' and 'theme' attributes, the XML gets generated ok. The problem then obviously is that every other page loses its styling. All this leads me to think that I'm approaching this wrong.
My question is: what's an accepted way to generate XML in C#, for consumption by a jQuery AJAX call, say?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我返回简单的数据(不是页面),我可能不会使用 aspx;这确实是网络表单,但您返回的不是网络表单。我想到了两个选择:
HttpContext
来构造您的响应您还可以尝试(在 aspx 内)清除响应(
Clear()
?)并随后调用Close()
。但在我看来,比仅仅使用处理程序要迂回得多。If I am returning simple data (not a page), I probably wouldn't use aspx; that is really web-forms, but what you are returning isn't a web-form. Two options leap to mind:
HttpContext
with which to construct your responseYou could also try (within aspx) clearing the response (
Clear()
?) and callingClose()
afterwards. But IMO a lot more roundabout than just using a handler.您需要使用主题=“”
例子:
You need to use theme=""
example:
尝试写入
Response.OutputStream
:Try writing to the
Response.OutputStream
instead: