Click here to Skip to main content
13,345,275 members (46,813 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 8 Sep 2012

Call .NET WebAPIs using matching JavaScript functions - JSRPC.Net

, 8 Sep 2012
Rate this:
Please Sign up or sign in to vote.
JSRPC.Net creates a JavaScript object with functions to call your WebAPI.


Microsoft's new MVC 4 has a great new feature, WebAPI (tutorial), that makes returning data (instead of an ActionResult) much easier. There's a bit of room for improvement, however. The problem and solution is easiest to see with a side-by-side comparison. First, some code with stock MVC 4:

class DataTransferObject
    public int left{get;set;}
    public int right {get;set;}
class ExampleController : ApiController
    public int Add(DataTransferObject dto)
        return dto.left + dto.right;

And to access it from JavaScript:

var result;
var dto = new Object();
dto.left = 3;
dto.right = 5;
    cache: false,
    async: false,
    type: "POST",
    url: "/api/Example/Add",
    contentType: 'application/json',
    dataType: "json",
    data: JSON.stringify(dto),
    success: function (receivedData) { result = receivedData; }

This is not a very elegant solution for what is essentially nothing more than a remote procedure call (RPC). Now for the same thing, using JSRPC.Net:

public class ExampleController : JSRPCNet.ApiController
    public int Add(int left, int right)
        return left + right;

And the corresponding JavaScript:

var api = new JSRPCNet("/api/Example");
alert(api.Add(3, 5));

Or, asynchronously:

var api = new JSRPCNet("/api/Example");
api.AddAsync(3, 5, function (result)

Using the code 

  • Instead of using System.Web.Http.ApiController as the base for your API classes, use JSRPCNet::ApiController as shown above.
  • Also, mark API methods with the [ApiMethod] attribute, instead of HttpPost.
  • Ensure that the controller class name ends with Controller, but exclude it from the API URL.
  • The routing string must be: "api/{controller}/{action}/{id}".
  • The api part is not actually required, so long as the API URL you use is correct. 
  • Reference JSRPCNet.js from your .cshmtl wherever appropriate. 
  • If possible, keep a copy of the constructed JSRPCNet object in JavaScript, to improve performance. 
  • Each JavaScript function has an async equivalent that takes a function object as its last parameter.  

Points of interest  

The class JSRPCNet::ApiController has a post method called GetAPI. When calling the new JSRPCNet(apiURL) in JavaScript, it uses this method to retrieve a listing of [ApiMethod] methods and their parameter names, and builds the JavaScript functions using the received data. Some things to note: 

  • JSRPCNet::ApiController caches compiled API information and late-bound methods to improve performance.
  • All API calls are performed using POST.
  • The parameters may be primitive types or complex types - anything that can serialized and deserialized as JSON. 

Some technologies that make it all work:

  • .NET's Reflection
  • Expression trees to compile late-bound dynamic calls, so calls are fast
  • JSON.Net and C#'s dynamic keyword.
  • MVC4's WebAPI.
  • jQuery's $.ajax function.
  • LINQ to manipulate data collections 


  • 8/29/2012 - Initial upload.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Question[My vote of 1] Just source files Pin
gordingin12-Sep-12 23:20
membergordingin12-Sep-12 23:20 
AnswerRe: [My vote of 1] Just source files Pin
coder25513-Sep-12 9:26
membercoder25513-Sep-12 9:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 8 Sep 2012
Article Copyright 2012 by coder255
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid