estudiante
estudiante

Reputation: 45

How can I return a json from c# but remove the response d:null

how can I remove d:null from my service response? I have tried many options and it continues to return the value d:null and I don't know what else can be done to solve the problem

I add my c# code, the configuration of my web.config and the response of the service, where you can see the correct response response:200 but it continues adding the value d:null

[WebMethod]
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

        public void test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;


            Context.Response.Write(js.Serialize(des));
        }


<system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="DebugJSonBehavior" >
                    <enableWebScript />
                    <!--need set automaticFormatSelectionEnabled attribute -->
                    <webHttp automaticFormatSelectionEnabled="true" />
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="DebugJSonBehavior" >
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

    </system.serviceModel>

this is my response when i call the service from postman { "response": "200" }{ "d": null }

thanks

Upvotes: 0

Views: 713

Answers (4)

Santosh Kumar Bind
Santosh Kumar Bind

Reputation: 63

 this is  works for me also.

 ResponseError err = new ResponseError();
   List<ResponseError> errorDetails = new List<ResponseError>();
   public class ResponseError
    {
        //public List<ErrorDetails> Error { get; set; }
        public string ErrorCode { get; set; }
        public string ErrorMessage { get; set; }
        public string ISACTicketNumber { get; set; }
    }
       errorDetails.Add(err);

            
            var outputJson = err;
JavaScriptSerializer Js = new JavaScriptSerializer();
            var jresp = JsonConvert.SerializeObject(outputJson, Formatting.Indented);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType ="application/json; charset=utf-8";
            HttpContext.Current.Response.Write(Js.Serialize(outputJson));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            //HttpContext.Current.Response.End();
           
            return outputJson;

Upvotes: 0

Burak Horozoğlu
Burak Horozoğlu

Reputation: 1

[WebMethod]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]

    public void Locations(string Location)
    {
        JavaScriptSerializer Js = new JavaScriptSerializer();
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        
        Query = "Select xxxx";
        DataTable Dt = Func.GetDataTable(Query);

        if (Dt.Rows.Count == 0)
        {
            LocationsCs Data = new LocationsCs();
            Data.Id = "0";
            Data.Location = "0";
            Context.Response.Write(Js.Serialize(Data));
        }
        else
        {
            List<LocationsCs> Locations = new List<LocationsCs>();
            for (int i = 0; i < Dt.Rows.Count; i++)
            {
                Locations.Add(new LocationsCs
                {
                    Id = Dt.Rows[i]["Id"].ToString(),
                    Location = Dt.Rows[i]["Location"].ToString(),
                });
            }
            //string  strResponse = Js.Serialize(Data);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
            HttpContext.Current.Response.Write(Js.Serialize(Locations));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
    }

    public class LocationsCs
    {
        public String Id;
        public String Location;
    }

this works for me.

Upvotes: 0

estudiante
estudiante

Reputation: 45

this was my solution

        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void test(string number)
        {
          
        
            WebException webException = null;

            var js = new System.Web.Script.Serialization.JavaScriptSerializer();
            Resultres = new Result();
            res.response = "ok";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
            HttpContext.Current.Response.Write(js.Serialize(res));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }

Upvotes: 2

estudiante
estudiante

Reputation: 45

With this code it returns me what you see in the image where d:null is added at the end of the response in postman

 [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
       
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;

            Response res = new Response();
            res.response = "Apple";
            string json = JsonConvert.SerializeObject(res);
            Context.Response.Write(json);
        }

enter image description here

now returning a string postman keeps including the d in the response and I don't know how to remove d:

 [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public string test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
       
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;

            Response res = new Response();
            res.response = "Apple";
            string json = JsonConvert.SerializeObject(res);
            return json;
            //Context.Response.Write(json);
        }

response postman enter image description here

Upvotes: 1

Related Questions