WebService with JSON implementation

Task: WebService with JSON implementation in asp.net

Description: Here sample of WebService implementation in asp.net which returns only JSON format, default format of webservice is xml. But we have convert the xml response into JSON format here. As nowdays we need to JSON for IPHON and Android App. Also we can use WebApi which is latest technology. Which we will describe later. 



using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.IO;
/// <summary>
/// Summary description for MyWebService
/// </summary>
[WebService(Namespace = "http://www.weburl.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 [System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService {
   
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void ShowUserDetail(string userid, string accesstoken)
    {
        MyUser myuser = new MyUser();
        List<MyUser> buser = new List<MyUser>();

        bool status = myuser.ShowProfile(userid, accesstoken);
        if (status)
        {
            Status = "Success";
            StatusMessage = "Success With Data";
            buser.Add(bringuser);
        }
        else
        {
            Status = "Failed";
            StatusMessage = "Authentication Failed";
        }

        var resp = new JSONEnvelope<MyUser>(buser, Status, StatusMessage);

        JavaScriptSerializer Machinejson = new JavaScriptSerializer();
        this.Context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
        this.Context.Response.ContentType = "application/json; charset=utf-8"; // to remove xml tag from response
        this.Context.Response.Write(Machinejson.Serialize(resp));
    }

}


Here is Class for getting Data from database . 


/// <summary>
/// Summary description for MyUser
/// </summary>
public class MyUser
{
      public int Id { get; set; }
      public string UserName { get; set; }
      public string firstName { get; set; }
      public string Email { get; set; }
      public string phone { get; set; }
      public string profile_pic_url { get; set; }
      public string shortdescrition { get; set; }

public bool ShowProfile(string userid, string accesstoken)
      {
          DataLayer dl = new DataLayer();
          DataTable dt = dl.GetProfile(useridaccesstoken ); // Return Datatable
          if (dt.Rows.Count > 0)
          {
              Id = Convert.ToInt32(dt.Rows[0]["id"]);
              UserName = dt.Rows[0]["username"].ToString();
              firstName = dt.Rows[0]["firstname"].ToString();
              Email = dt.Rows[0]["email"].ToString();
              phone = dt.Rows[0]["phone"].ToString();
              profile_pic_url = dt.Rows[0]["profile_pic_url"].ToString();
              shortdescrition = dt.Rows[0]["shortdescription"].ToString();
              return true;
          }
          return false;
      }
}
 


Here Class for generating a Envelope for JSON response. 


public class JSONEnvelope<T>
{
    public List<T> Data { get; private set; }
    public string Status { get; set; }
    public string StatusMessage { get; set; }

    public JSONEnvelope(IEnumerable<T> items, string status, string statusmessage)
    {
        Data = new List<T>(items);
        Status = status;
        StatusMessage = statusmessage;
    }
}

1 comment: