Demo Page: http://examples.nickriggs.com/mvcapi/

ASP.NET MVC Action Filter exposes your controller’s actions as Json and Xml API endpoints. Simply decorate your controller or specific actions with the Api attribute:

[Api]
public ActionResult Create(Person person)
{
    person.Id = Guid.NewGuid();
    _people.Add(person);

    return View(person);
}
Specify the format of your HTTP request using the Content-Type header and the expected format of the response using the Accept header.

This request sends Json and returns Json:

Request:
POST http://localhost.:33325/Home/Create HTTP/1.1
Host: localhost.:33325
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: application/json, text/javascript, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: text/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost.:33325/
Content-Length: 48
Cookie: newUser=false
Pragma: no-cache
Cache-Control: no-cache

{"FirstName":"Nick","LastName":"Riggs","Age":30}

Response:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 10 Aug 2010 23:35:27 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 2.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 92
Connection: Close

{"Id":"0e59c4a8-4bf0-4885-b062-426929d7b5d2","FirstName":"Nick","LastName":"Riggs","Age":30}

jQuery Example:
$.ajax({
   type: "POST",
   contentType: "text/json",
   dataType: "json",
   data: "{\"FirstName\":\"Nick\",\"LastName\":\"Riggs\",\"Age\":30}",
   url: "Home/Create"
});

Last edited Aug 11, 2010 at 12:05 AM by nickriggs, version 10