当前位置: 动力学知识库 > 问答 > 编程问答 >

.net 2.0 - Is there a recommended ASP.NET 2.0 method to serve HTML snippets via XHR to the client?

问题描述:

Here is my desired flow:

  1. A static web page (html) uses XHR calls to an ASP.NET page
  2. The .NET page retrieves information from a remote server using web services
  3. The .NET page returns an HTML "snippet" that is inserted into the static HTML page

I'm getting hung up on how to deal with the HTML snippet generation on the .NET (2.0) page. I've thought about something like this in a generic .ashx page:

public void ProcessRequest (HttpContext context)

{

context.Response.Write("<ul>");

//assume "people" is a list of data coming from the external web service

foreach (string person in people)

{

context.Response.Write("<li>" + person + "</li>");

}

context.Response.Write("</ul>");

}

It just seems a big "ugly". Has anyone done this another - and possibly more efficient/elegant - way? Any help would be appreciated.

网友答案:

Returning html for this task is a bit weird, IMO. In most times I prefer the following way. Open your web service to public or add a wrapper to it and just use it directly from js of your static page. The service should return json (preferable) or xml data. On the client side format (print in html as you want) received data using js in callback to the XHR and inject anywhere you want.
But also I want to cast YAGNI on this task - if it'll be used only several times and in a few pages, use the most fastest way to implement it. But if you are building some RIA application I recommend you to check ExtJS javascript library.

Edit 26/02:
If you can't use ASP.NET MVC but wanna to use some good framework instead of "Response.Write" stuff please check OpenRasta. It's one of my favorite web frameworks. It works fine on .Net 2.0 and it's very flexible and powerful. And also it has a great community.

分享给朋友:
您可能感兴趣的文章:
随机阅读: