Simplest Possible ASP .NET AJAX Proxy Page
After spending many hours poking around trying to get an ASP .NET AJAX proxy page, I'm pretty sure someone out there knows of an easier & simpler way.
These are two ways I have tried:
- Web services endpoint.
Problem: Super complicated, lots of work
- An OnLoad handler that sucks in a URL parameter and spits out the desired webpage (or JSON or XML).
Problem: The code is never called.
Secondary Problem: Setting up a proxy page should not require writing a bunch of code (aka, do we really need to keep reinventing an admitt开发者_StackOverflow中文版edly simple wheel?) and generating 2 different files (the ASPX and the code-behind)
What is the simplest way to make an ASP .NET AJAX proxy page?
Meta-note: I realize this treads a bit close to a discussion topic. Alas, I cannot flag this as a community wiki question. If you think it should be a wiki question, please mark it for me.
You can use a generic HTTP handler (ashx file). Quick example:
<%@ WebHandler Language="C#" Class="Proxy" %>
using System.Web;
using System.Net;
public class Proxy : IHttpHandler {
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/plain";
using (WebClient client = new WebClient()) {
context.Response.BinaryWrite(client.DownloadData(context.Request.QueryString["url"]));
}
}
public bool IsReusable { get { return true; } }
}
精彩评论