开发者

Simplest Possible ASP .NET AJAX Proxy Page

开发者 https://www.devze.com 2023-01-21 18:55 出处:网络
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.

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:

  1. Web services endpoint.

Problem: Super complicated, lots of work

  1. 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; } }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消