background preloader

JSON API

Facebook Twitter

API v1.2. Expose your Existing ASP.NET MVC Actions as JSON/XML API Endpoints - Nick Riggs, Web Developer. Why write separate actions to provide a JSON/XML API in ASP.NET MVC?

Expose your Existing ASP.NET MVC Actions as JSON/XML API Endpoints - Nick Riggs, Web Developer

If you are already using view models, MVC API Action Filter makes reusing your existing actions quick and clean. Often times I’d like to provide simple JSON/XML API endpoints for my web application. When I first started using ASP.NET MVC, I’d create separate actions that would return a JSONActionResult. This worked, but always felt like code bloat in my controllers. I also am an avid user of view models in my controllers – as models for my views and as parameters for my actions. This got me thinking that my existing controller actions were already poised to be my API. As an example, let’s have a Create action that accepts a Person view model, modifies the databases and redirects to the Details action: This works fine using the Html form and displaying the details after the post: Now let’s take the same actions and expose them as JSON/XML API endpoints.

The response looks like: An XML example: Build Truly RESTful API and Website using Same ASP.NET MVC Code. Download the library from here.

Build Truly RESTful API and Website using Same ASP.NET MVC Code

Introduction A truly RESTful API means you have unique URLs to uniquely represent entities and collections, and there is no verb/action on the URL. You cannot have URLs like /Customers/Create or /Customers/John/Update, /Customers/John/Delete where the action is part of the URL that represents the entity. A URL can only represent the state of an entity, like /Customers/John represents the state of John - a customer, and allow GET, POST, PUT, DELETE on that very URL to perform CRUD operations.

Same goes for a collection where /Customers returns a list of customers and a POST to that URL adds new customer(s). I have tried Scott Gu’s examples on creating RESTful routes, this MSDN Magazine article, Phil Haack’s REST SDK for ASP.NET MVC, and various other examples. How To Create A JSON Web Service In ASP.NET. ASP.NET makes it easy to create web services but they usually return XML.

How To Create A JSON Web Service In ASP.NET

Like many web developers I now prefer JSON. This article and sample code will show you how to get your web service to return data in the JSON format. The web service is written in C#. Since my web hosting company only provides me with a MySQL database, we’ll make this project a little more challenging by pulling data from a MySQL database. Finally, I will show you how to use jQuery to call your web service. The first task to tackle is connecting to the MySQL database. 1: <connectionStrings> 2: <add name="Books" connectionString="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=books;uid=root;pwd=password;option=3;" providerName="System.Data.Odbc"/> 3: </connectionStrings> NOTE: This is not my real password and there is no external access to MySQL server running on my development server.

For this demonstration, I am using a simple database I use to keep track of the books I read. 1: using System; 37: int i = 0;