在C#中为jQuery创建XML [英] Creating XML in C# for jQuery

查看:61
本文介绍了在C#中为jQuery创建XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为jQuery.get(AJAX)调用生成一些XML,并且从我的C#页面中收到以下错误:使用主题化的CSS文件需要页面上的标头控件. (例如<head runat="server" />)."

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" />)."

生成XML的文件是一个简单的.aspx文件,完全由以下内容组成:

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

使用Linq-to-XML进行代码隐藏,可以正常工作:

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());

我知道该错误与Web.Config的<pages styleSheetTheme="default" theme="default">标记有关,因为当我删除'styleSheetTheme'和'theme'属性时,XML会正常生成.显然,问题在于其他所有页面都失去了样式.所有这些使我认为我正在解决这个错误.

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.

我的问题是:用C#生成XML以接受jQuery AJAX调用的一种可接受的方式是什么?

My question is: what's an accepted way to generate XML in C#, for consumption by a jQuery AJAX call, say?

推荐答案

如果我要返回简单数据(而不是页面),则可能不会使用aspx.那确实是网络表格,但是您返回的不是网络表格.想到两个选择:

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:

  • 使用ASP.NET MVC;听起来很老套,但确实可以更好地返回不同类型的响应
  • 使用处理程序(ashx)-忽略了所有网络形式的噪音,只剩下一个HttpContext即可用来构建响应
  • 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

您也可以尝试(aspx内)清除响应(Clear()?),然后再调用Close().但是,IMO不仅仅是使用处理程序,还具有更多的回旋处.

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.

这篇关于在C#中为jQuery创建XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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