In this article, I am going to explain you how to create a simple WebAPI application and post json data to WebAPI using C#. We will be using Visual Studio 2013 and .NET framework 4.5.
We will cover below topics.
1) Creating an empty WebAPI application.
2) Creating a web api method which takes complex data (Employee or Person...etc.) as input parameter and returns single string in response.
3) Passing jSON data to complex method in C# using Http WebRequest.
4) Passing jSON data to complex method in C# using HttpClient.
Creating Web API Empty Application
First step is to create ASP.NET Web API empty application as shown below.
Go to File → New → Project. A new window will be open as shown below.
Now go to Web and select .NET Framework 4.5 and give project name and click on OK .
Now new window will open as shown below.
Now Select Empty Template, check on Web API checkbox and click on OK.
Now, a new project will be created as shown below.
Modify Web API Routing
Open WebApiConfig.cs file. Visual studio generates default routing as shown below. Add {action} in routeTemplate as highlighted in yellow.
Next step is to add api controller to application. Go to Controller folder → Add → Controller.. → select 'Web API 2 Controller-Empty'. Give it's name HomeController and click on Add. In home controller class, create a method SaveEmployee whick takes Employee object as input parameter and returns simple string.
WebAPI method supports below return types.
Void
Primitive type or Complex type
HttpResponseMessage
IHttpActionResult
We will be using HttpResponseMessage method type in this demo. With HttpResponseMessage, we can return response data (simple or complex objects) along with different status code like 202, 403, 404 as per our requirement.
Passing jSON data to complex method in C# using Http WebRequest
Make sure webapi application is running. Now, create a C# console application and pass complex json data to WebAPI method using http WebRequest in C#.
Before passing complex objects, we need to serialize complex data into jSON object. For this you can use Json.NET - Newtonsoft or JavaScriptSerializer. In this demo, we will be using JavaScriptSerializer.
To use Json.NET, you can download dll from internet or you can add reference from nuget package manager.
To use JavaScriptSerializer, we need to add System.Web.Extensions assembly reference to client project.
using System;
using
System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using
System.Threading.Tasks;
using
System.Web.Script.Serialization;
namespace ConsoleApplication1
{
classProgram
{
staticvoid Main(string[] args)
{
Test test = newTest();
string
output =
test.PassComplexData();
}
}
publicclassEmployee
{
publicstring Name { get; set; }
publicint Age { get; set; }
}
publicclassTest
{
publicstring PassComplexData()
{
string ResponseString = "";
HttpWebResponse
response
=
null;
try
{
var request = (HttpWebRequest)WebRequest.Create("http://localhost:32160/api/home/SaveEmployee");
Passing jSON data to complex method in C# using HttpClient
To use HttpClient, we need to add System.Net.Http assembly reference to client project and also to use JavaScriptSerializer, we need to add System.Web.Extensions assembly reference to client project.
OUTPUT: Output will be same as http WebRequest as seen above.
Modifying HttpResponseMessage SaveEmployee Method
Till now, we were returning single string from web api method using HttpResponseMessage type. Now modify SaveEmployee as shown below to return complex data in json or xml format.