在 C# 中为 jQuery 创建 XML

发布于 2024-08-06 10:29:47 字数 1002 浏览 3 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2024-08-13 10:29:47

如果我返回简单的数据(不是页面),我可能不会使用 aspx;这确实是网络表单,但您返回的不是网络表单。我想到了两个选择:

  • 使用 ASP.NET MVC;听起来很老套,但它确实准备好
  • 使用处理程序(ashx)更优雅地返回不同类型的响应 - 它省略了所有 Web 表单噪音,只留下一个 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:

  • use ASP.NET MVC; sounds corny, but it really is geared up to return different types of response much more elegantly
  • use a handler (ashx) - which omits all the web-form noise, just leaving you with a HttpContext with which to construct your response

You could also try (within aspx) clearing the response (Clear()?) and calling Close() afterwards. But IMO a lot more roundabout than just using a handler.

勿忘初心 2024-08-13 10:29:47

您需要使用主题=“”
例子:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChangePeopleService.aspx.cs" Inherits="ChangeRegister.Person.ChangePeopleService" Theme="" %>

You need to use theme=""
example:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChangePeopleService.aspx.cs" Inherits="ChangeRegister.Person.ChangePeopleService" Theme="" %>
清引 2024-08-13 10:29:47

尝试写入Response.OutputStream

HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

using (TextWriter textWriter 
    = new StreamWriter(HttpContext.Current.Response.OutputStream, Encoding.UTF8))
{
    XmlTextWriter writer = new XmlTextWriter(textWriter);
    writer.WriteString(xml.ToString());
}

Try writing to the Response.OutputStream instead:

HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

using (TextWriter textWriter 
    = new StreamWriter(HttpContext.Current.Response.OutputStream, Encoding.UTF8))
{
    XmlTextWriter writer = new XmlTextWriter(textWriter);
    writer.WriteString(xml.ToString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文