Advertisement
Guest User

C# IHttpHandler JSON service

a guest
Oct 29th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Web;
  7. using System.IO;
  8. using Newtonsoft.JSON; // Use the JSON.NET library from NuGet in your project here!
  9.  
  10. public class InputObject
  11. {
  12.     // Put each parameter you are passing from client side here
  13.     // Can use arrays + more
  14.     // Read up details in JSON.NET library documentation
  15.     public string ParameterOne;
  16.     public string ParameterTwo;
  17. }
  18.  
  19. public class OutputObject
  20. {
  21.     // Put each parameter you are passing back to client side here
  22.     // Can you use arrays + more
  23.     // Read up details in JSON.NET library documentation
  24.     public string ParameterOne;
  25.     public string ParameterTwo;
  26. }
  27.  
  28. public class Handler1 : System.Web.IHttpHandler
  29. {
  30.     public void ProcessRequest(HttpContext context)
  31.     {
  32.         InputObject input = JsonConvert.DeserializeObject<InputObject>(StreamToString(context.Request.InputStream));
  33.  
  34.         OutputObject resultObject = null;
  35.         // populate your resultObject here by fetching data from the database
  36.  
  37.         context.Response.ContentType = "text/json";
  38.         context.Response.Write(JsonConvert.SerializeObject(resultObject));
  39.  
  40.     }
  41.  
  42.     public bool IsReusable {
  43.         get { return false; }
  44.     }
  45.  
  46.     private string StreamToString(Stream target)
  47.     {
  48.         string resultString = "";
  49.         using (StreamReader reader = new StreamReader(target)) {
  50.             resultString = reader.ReadToEnd();
  51.         }
  52.         return resultString;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement