簡體   English   中英

從C#Web服務中刪除xml標頭

[英]Remove xml header from C# webservice

我需要使用C#網絡服務構建API,該API需要以json格式返回值。

目前,我有以下幾行代碼

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return new JavaScriptSerializer().Serialize(new { errMsg = "test" });
        }
    }
}

調用post方法時的輸出為

<?xml version="1.0" encoding="utf-8"?>

<string xmlns="http://tempuri.org/">{"errMsg":"test"}</string>

但這不是有效的json,如何使Web服務僅返回json對象,而不返回xml標頭?

使用標題Content-Type:application / json調用此Web服務將自動將響應轉換為json,並且您的xml將消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM