Skip to content

WebApi Proxy Provider

Fanie Reynders edited this page Jan 22, 2015 · 4 revisions

Getting started

Install via NuGet

Extend your ASP.NET Web API service by installing this package from NuGet:

Install-Package WebApiProxy

Note: This package requires the core libraries of ASP.NET Web API (version 5 or higher)

Register proxy endpoint

You need to explicitly register the proxy endpoint route. You can do this by using the RegisterProxyRoutes extension method that extends HttpConfiguration:

config.RegisterProxyRoutes();

Note: Make sure to include the WebApiProxy.Server namespace

This will register your service with an additional endpoint with the default address as /api/proxies'. It is also possible to specify a custom address by passing the routeTemplate` parameter:

config.RegisterProxyRoutes("$metadata");

Usage

This extension provides a proxy endpoint in your service (with /api/proxies as the default) that serves JavaScript and service metadata.

Given a Person API on the server:

public class PeopleController : ApiController
{
    public Person[] Get() {
    }

    public Person Get(int id) {
    }
}

allows you to use it like this in JavaScript on the client:

$.proxies.person.get()
  .done(function(people) {
    //do something with people
  });

$.proxies.person.get(2)
  .done(function(person) {
    //do something with person
  });

JavaScript proxy

Simply reference the proxy endpoint provided inside your HTML and you're good to go:

<script src="/api/proxies" type="text/javascript"></script>

This functionality was adopted from ProxyApi - kudos to Stephen Greatrex :)

Getting the service Metadata

Invoke the service on its proxy endpoint api/proxies with the request header X-Proxy-Type as "metadata" and the service metadata including documentation will be in the response.

Excluding controllers

You can exclude your controllers by simply decorating them with the ExcludeProxy attribute