Skip to content

Instantly share code, notes, and snippets.

@professionalsna
Last active June 9, 2017 11:46
Show Gist options
  • Save professionalsna/e7ea5c7cee0bef5d0f16b0f6edbcee75 to your computer and use it in GitHub Desktop.
Save professionalsna/e7ea5c7cee0bef5d0f16b0f6edbcee75 to your computer and use it in GitHub Desktop.
ASP.NET: Web service type implementation to return JSON
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
namespace TestingWebApp
{
/// <summary>
/// Summary description for ServiceHandler
/// </summary>
public class ServiceHandler : IHttpHandler
{
//key to access this webapi.
private const string ApiKey = "ApiKeyForTesting";
//private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// string jsonResult = @"{ 'status': 1,'response':[]}";
public void ProcessRequest(HttpContext context)
{
//provide return type of request
context.Response.ContentType = "application/json; charset=utf-8"; //"text/xml; charset=utf-8";
try
{
string appkey = context.Request.QueryString["api"];
string task = context.Request.QueryString["task"];
if (appkey!=ApiKey)
{
var res = new Result() { Status = 1, Response = "Invalid Request" };
var json = new JavaScriptSerializer();
context.Response.Write(json.Serialize(res));
return;
}
else
{
JObject job = new JObject();
//job.Property("status").Value = 1;
int statuscode = 0;
//JArray item = (JArray)job["response"];
string jsons="";
if (task == "GetName")
{
//get context pass it into method to generate json, paramters can be fetch using context
jsons = AuthorJson(context, ref statuscode);
if (statuscode == 1)
{
//parsing json to create json object i.e. string to json object
JObject jobj = JObject.Parse(jsons);
job.Add("response", jobj);
}
else {
job.Add("response", jsons);
}
}
else if (task=="Hello")
{
statuscode = 1;
jsons = "Hello World";
job.Add("response", jsons);
}
else {
jsons = "Invalid Command";
job.Add("response", jsons);
}
job.Add("status", statuscode);
context.Response.Write(job);
return;
}
}
catch
{
var res = new Result() { Status = 1, Response="Invalid Request"};
var json = new JavaScriptSerializer();
context.Response.Write(json.Serialize(res));
return;
}
}
public bool IsReusable
{
get
{
return false;
}
}
//Define Business Logic Here...
private string AuthorJson(HttpContext context,ref int statuscode)
{
string json = "";
//get Parameters from body post
var FirstName = context.Request["FirstName"];
var LastName = context.Request["LastName"];
if ((FirstName != null) && (LastName != null))
{
var author = new Author() { FirstName = FirstName,LastName=LastName };
List<Author> authors = new List<Author>();
authors.Add(author);
//genereate your response body here.... complext body can be created here..
json = @"{ 'Author': " + JsonConvert.SerializeObject(author, Formatting.Indented) +
",'AuthorsList':" + JsonConvert.SerializeObject(authors, Formatting.Indented) +
"}";
statuscode = 1;
}
else
{
json = "Invalid Parameters";
statuscode = 2;
}
return json;
}
}
public class Author
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Result {
public int Status { get; set; }
public string Response { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment