- MVC_JSON.zip
- MVC_JSON
- Common
- ConsoleApplication1
- MvcApplication1.sln
- MvcApplication1.suo
- packages
- HttpClient.0.5.0
- lib
- JsonValue.0.5.0
- JsonValue.0.5.0.nupkg
- lib
- 40
- Microsoft.Json.dll
- Microsoft.Server.Common.dll
- repositories.config
- RestfulWebSite
- App_Data
- bin
- Common.dll
- Common.pdb
- Microsoft.Json.dll
- Microsoft.Server.Common.dll
- RestfulWebSite.dll
- RestfulWebSite.pdb
- Content
- Site.css
- themes
- base
- images
- ui-bg_flat_0_aaaaaa_40x100.png
- ui-bg_flat_75_ffffff_40x100.png
- ui-bg_glass_55_fbf9ee_1x400.png
- ui-bg_glass_65_ffffff_1x400.png
- ui-bg_glass_75_dadada_1x400.png
- ui-bg_glass_75_e6e6e6_1x400.png
- ui-bg_glass_95_fef1ec_1x400.png
- ui-bg_highlight-soft_75_cccccc_1x100.png
- ui-icons_222222_256x240.png
- ui-icons_2e83ff_256x240.png
- ui-icons_454545_256x240.png
- ui-icons_888888_256x240.png
- ui-icons_cd0a0a_256x240.png
- jquery-ui.css
- Controllers
- Filter
- Global.asax
- Global.asax.cs
- ModelBinders
- obj
- Debug
- packages.config
- Properties
- RestfulWebSite.csproj
- RestfulWebSite.csproj.user
- Scripts
- Views
- Web.config
- Web.Debug.config
- Web.Release.config
- WEB_WCF.zip
- WEB_WCF
- Lib
- WCF Web API Preview 4
- Microsoft.ApplicationServer.Common.dll
- Microsoft.ApplicationServer.Common.XML
- Microsoft.ApplicationServer.Http.dll
- Microsoft.ApplicationServer.Http.XML
- Microsoft.ApplicationServer.HttpEnhancements.dll
- Microsoft.ApplicationServer.HttpEnhancements.Tests.dll
- Microsoft.ApplicationServer.HttpEnhancements.XML
- Microsoft.ApplicationServer.Serialization.dll
- Microsoft.ApplicationServer.Serialization.XML
- Microsoft.ApplicationServer.ServiceModel.dll
- Microsoft.ApplicationServer.ServiceModel.xml
- Microsoft.Net.Http.dll
- Microsoft.Net.Http.xml
- Microsoft.QueryComposition.dll
- Microsoft.Runtime.Serialization.Json.dll
- Microsoft.Runtime.Serialization.Json.xml
- Microsoft.ServiceModel.Web.jQuery.dll
- Microsoft.ServiceModel.Web.jQuery.xml
- Models
- RestFul_Test
- RESTFul_WCF
- WEB_WCF_TEST.sln
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using CodeStash.Filters;
using System.Json;
using RestfulWebSite.ModelBinders;
namespace RestfulWebSite.Controllers
{
/// <summary>
/// This controller shows 3 different methods of accepting JSON values
/// 1. Accepts JSON from JavaScript in the browser, and also from .NET client app using standard web APIs
/// 2. Accepts JSON from JavaScript in the browser
/// 3. Uses JSONValue API to accept JSON as dynamic object
/// </summary>
public class RestController : Controller
{
/// <summary>
/// Uses special ActionFilter to convert desktop JSON data
/// into acceptable action value. Returns standard JSON result
/// </summary>
[JSONFilter(Param = "person", RootType = typeof(Person))]
public ActionResult Index(Person person)
{
int age = person.Age;
List<Person> people = new List<Person>();
for (int i = 0; i < 5; i++)
{
string s = string.Format("{0}_{1}", "hardCoded", i.ToString());
people.Add(new Person(i, s, new Parent(1, "parent")));
}
return Json(people);
}
/// <summary>
/// Accepts standard JSON, from JavaScript call.
/// Returns standard JSON result
/// </summary>
public ActionResult NoFilterJSON(Person person)
{
int age = person.Age;
List<Person> people = new List<Person>();
for (int i = 0; i < 5; i++)
{
string s = string.Format("{0}_{1}", "hardCoded", i.ToString());
people.Add(new Person(i, s, new Parent(1, "parent")));
}
return Json(people);
}
/// <summary>
/// Uses JSONValue to convert to dynamic object.
/// Returns standard JSON result
/// </summary>
public ActionResult JsonValue(
[DynamicJson(typeof(Person))]
JsonValue person
)
{
var x = person.AsDynamic();
int age = x.Age;
List<Person> people = new List<Person>();
for (int i = 0; i < 5; i++)
{
string s = string.Format("{0}_{1}", "hardCoded", i.ToString());
people.Add(new Person(i, s, new Parent(1, "parent")));
}
return Json(people);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog