繁体   English   中英

如何从 c# 返回 json 但删除响应 d:null

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

如何从我的服务响应中删除 d:null? 我尝试了很多选项,它继续返回值 d:null,我不知道还能做些什么来解决这个问题

我添加了我的 c# 代码、我的 web.config 的配置和服务的响应,您可以在其中看到正确的响应 response:200 但它继续添加值 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>

这是我从邮递员 {"response": "200" }{ "d": null } 调用服务时的响应

谢谢

使用此代码,它会返回您在图像中看到的内容,其中 d:null 在邮递员的响应末尾添加

 [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);
        }

在此处输入图像描述

现在返回一个字符串邮递员一直在响应中包含 d,我不知道如何删除 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);
        }

回复邮递员在此处输入图像描述

这是我的解决方案

        [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();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM