You must Sign In to post a response.
  • Category: ASP.NET

    Best method to show response in json format?

    I am using web application.User can call my aspx page.I am taking input from user in the form of querystring.I want to give response to client in the form of json.Which is best method to show response in json format.

    try
    {
    string json = "message{\"description\":\""+description+"\"Id\":\""+id+"}";
    Response.Clear();
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(json);
    // Response.End();

    HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
    HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
    HttpContext.Current.ApplicationInstance.CompleteRequest();

    //HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    catch(Exception ex)
    {
    Response.Write(ex.ToString());
    }

    or create a class and do serilize.Which one is best method?
  • #768814
    Hai Pinky,
    There are various methods through which you can return the JSON result from the response.
    Once you get the response, you can also convert it or de-serialize it to the JSON format.

    HttpResponseMessage response = null;
    string path = "";
    if (System.Configuration.ConfigurationManager.AppSettings["FileUploadPath"] != null)
    {
    path = System.Configuration.ConfigurationManager.AppSettings["FileUploadPath"].ToString();
    }
    var header = System.Web.HttpContext.Current.Request.Headers["FileData"];
    FileUplaodRequest fileUploadData = JsonConvert.DeserializeObject<FileUplaodRequest>(header);

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments