The Freak Parade

Strange noises from the mind of Nathan Stults…
  • rss
  • Home
  • About The Freak Parade

Simple ASP.NET MVC Ajax Proxy

February 24, 2009

You may find, as you begin to combine the incredible power of jQuery, OpenLayers, and other JavaScript libraries capable of asynchronous (AJAX) behavior, that you want to serve some data to your client side code that doesn’t originate from your ASP.NET MVC application. Most commonly, I imagine, this need arises due to the fact that for security reasons browsers don’t allow AJAX calls to be made to domains external to the web app that served the original web page. The well known solution to this problem is to have your client side code make a request to your web application which will then make the request to the external resource and pass the result back to the client side AJAX request. There are other reasons, besides permitting cross domain AJAX calls, that you may want to use such a proxy. In our case we are hosting a GeoServer instance that serves web maps displaying proprietary, strategic customer spatial data. GeoServer has a relatively limited security model, at least in terms of remote administration, so we wanted to hide the GeoServer instance behind our firewall and layer our own, more sophisticated, role based security.

Whatever your motivations, this is a trivial feature to implement in almost any environment, and ASP.NET MVC is no exception. ASP.NET MVC’s flexible design also makes it possible to implement this kind of proxy in a very reusable, application-developer friendly way, reducing the code required on your controller to a single line. Of course, you could put your proxy in an HTTP (.axd) module and reduce the line count to zero lines of code if you wanted, but you may not want to open up a generic, free for all proxy mechanism to the client, for the same reasons that the cross domain restriction was created in the first place. I stole part of this solution from here. Here’s the code:

    public class ProxyResult : ViewResult
    {
        public ProxyResult(Uri targetUri)
        {
            TargetUri = targetUri;
        }

        public ProxyResult(string baseUrl,string queryString)
        {
            TargetUri = new Uri(baseUrl + "?" + queryString);
        }

        public Uri TargetUri
        {
            get; set;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            WebRequest proxy = WebRequest.Create(TargetUri);
            using (WebResponse proxyResponse = proxy.GetResponse())
            {
                context.HttpContext.Response.ContentType = proxyResponse.ContentType;
                using (Stream proxyResponseStream = proxyResponse.GetResponseStream())
                {
                    StreamHelper.CopyStream(proxyResponseStream, context.HttpContext.Response.OutputStream);
                }
            }
        }
    }

    public static class StreamHelper
    {
        public static void CopyStream(Stream input, Stream output)
        {
            var buffer = new byte[32768];
            while (true)
            {
                int read = input.Read(buffer, 0, buffer.Length);
                if (read < = 0)
                    return;
                output.Write(buffer, 0, read);
            }

        }

    }

To provide proxied access to a specific, known url to your client side code via your controller:

    public class MapController : SecuredController
    {
        private readonly string _wmsServerUrl = ConfigurationManager.AppSettings["GeoServerUrl"] + "/wms";
        private readonly string _wfsServerUrl = ConfigurationManager.AppSettings["GeoServerUrl"] + "/wfs";

        public ActionResult Show()
        {
            return View("Map");
        }

        public ProxyResult WmsProxy()
        {
            return new ProxyResult(_wmsServerUrl, Request.QueryString.ToString());
        }

        public ProxyResult WfsProxy()
        {
            return new ProxyResult(_wfsServerUrl, Request.QueryString.ToString());
        }
    }

And in your client side javascipt code, you simply reference your local web app, and whatever query string would normally be required:


points = new OpenLayers.Layer.WMS(
            "Units",
            "/Map/WmsProxy",
            {
                layers: 'hsi:someprivatelayer',
                styles: '',
                srs: 'EPSG:4326',
                format: 'image/png',
                tiled: 'true',
                tilesOrigin: "143.60260815000004,-43.851764249999995",
                transparent: true
            },
            {
                'opacity': 1,
                'isBaseLayer': false,
                'wrapDateLine': true
            }
        );
Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • DZone
  • Digg
  • Google Bookmarks
  • Ma.gnolia
  • Technorati
hello
Categories
General
Comments rss
Comments rss
Trackback
Trackback

« The incredible, fallible symmetric Unit Test, and the beauty of Open Source software. Lucy in the Sky with Ruby »

  • chris patterson
    Nice.

    When does a method like this need to be implemented versus taking advantage of something like JSONP?
  • nstults
    My understanding of JSONP is that the script that wants to access remote data (which is expected to be javascript, I presume, whereas in this example the payload is a binary (png) stream) needs to be aware of the JSONP arrangement, whereas in this case the goal is allow a third party component (OpenLayers) interact with a map server on a different domain without having to write and specific client side or server side glue code to make it happen, we can just give the OpenLayers control a url to a WMS server and everything works as expected. Also with JSONP (as I understand it) the cross-domain resource needs to be aware of the protocol, whereas with a proxy, the cross-domain resource does not need any modification whatsoever. But I only briefly looked at JSONP at your mention, so I have a very light understanding of its capabilities.
  • chris
    I did some research after reading your post. You are correct. The resource on the other end of the XSS call needs to be aware of the JSONP arrangement. It must "pad" the response with the call to the callback function. Rick Strahl has a good write up on the subject.

    http://www.west-wind.com/Weblog/posts/107136.aspx
blog comments powered by Disqus

Subscribe

Calendar

February 2009
M T W T F S S
« Nov   Oct »
 1
2345678
9101112131415
16171819202122
232425262728  

Recent Posts

  • So you want to learn IronRuby? – Chapter 1 – “The Back Story”
  • Lucy in the Sky with Ruby
  • Simple ASP.NET MVC Ajax Proxy
  • The incredible, fallible symmetric Unit Test, and the beauty of Open Source software.
  • You Can’t Fill an Imaginary Hole

Recent Comments

  • nstults on Content Management Systems (CMS) for the .NET Platform
  • hayamayu on Content Management Systems (CMS) for the .NET Platform
  • ADSL Viettel on Evaluating Expressions at Runtime in .NET (C#)
  • Swing Trading on CrapOverflow? Really? Oooooooook then… (Or, the fallacy of elitism)
  • nstults on So you want to learn IronRuby? – Chapter 1 – “The Back Story”

Tags

TDD Testing

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox