Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

What I have tried:

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());
        }
Posted
Updated 12-Jan-17 1:02am
Comments
Afzaal Ahmad Zeeshan 12-Jan-17 7:45am    
Apart from what others have said, I would recommend using Web API if you want to consider generating responses in JSON or XML format. Web APIs are very easy to program, and only take required data as input and generate only require data as output — nothing more, nothing less.

Hi,
You can use following method to convert JSON string from Object.
C#
public class Message
{
    public string Description { get; set; }
    public int Id { get; set; }  
}
Message myReturnData = new Message() { Description = "This is test description", Id = 1 };
string json = JsonConvert.SerializeObject(myReturnData);

You must have reference of newtonsoft JSON Library Introduction[^]

Regards,
Imdadhusen
 
Share this answer
 
Comments
Karthik_Mahalingam 2-Feb-17 2:57am    
5
Hi
I would recommend you change your method to [WebMethod] with return type of Dynamic/Object and serialize your results to JSON.

Send your requests to that web method using AJAX.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900