WebService返回文本JSON数据格式

WebService返回的格式都是xml

Markup
<string xmlns="">Hello Worldstring>

 在前段js处理时需要先解析xml

JavaScript
$.parseXML(xmlstr).find("string").text();

处理起来费劲还有兼容性问题 

现在网站都已经返回json格式了

Markup
<string xmlns="http://tempuri.org/">{"Name":"张三","Age":18}string>

 还是需要先处理xml在转化json字符串

让WebService直接返回JSON数据格式方法一:

C#
    [WebMethod]

    public void HelloWorld()

    { 

        string str= "Hello World"; 

        Context.Response.Write(str);

        Context.Response.End();

    }

能直接得到字符串格式,缺点就是每个方法都需要写Context.Response.Write和Context.Response.End

让WebService直接返回JSON数据格式方法二:

在Web.config中增加

C#
    <modules>

      <add name="AsmxRequestModule" type="AsmxRequestModule"/>

    </modules>

IIS经典模式 是httpModules

在appcode新增类文件

C#
public class AsmxRequestModule : IHttpModule

{

    public void Init(HttpApplication context)

    {

        context.BeginRequest += new EventHandler(Application_BeginRequest);

    }

    public void Dispose()

    {

    }

    public void Application_BeginRequest(object sender, EventArgs e)

    {

        HttpApplication application = sender as HttpApplication;

        string extension = Path.GetExtension(application.Request.Path);

        if (application.Request.Path.IndexOf("/WebService.asmx/") > -1)

        {

            application.Response.Filter = new CatchTextStream(application.Response.Filter);

        }

    }

}
C#
public class CatchTextStream : Stream
{
    #region 数据流
    /// 
    /// 数据流
    /// 
    private Stream output;
    #endregion
    #region 构造函数
    /// 
    /// 构造函数
    /// 
    /// 
    /// 
    /// 
    /// 
    public CatchTextStream(Stream s)
    {
        output = s;
    }
    #endregion
    #region 重载属性及方法
    public override bool CanRead
    {
        get { return output.CanRead; }
    }
    public override bool CanSeek
    {
        get { return output.CanSeek; }
    }
    public override bool CanWrite
    {
        get { return output.CanWrite; }
    }
    public override void Flush()
    {
        output.Flush();
    }
    public override long Length
    {
        get { return output.Length; }
    }
    public override long Position
    {
        get { return output.Position; }
        set { output.Position = value; }
    }
    public override int Read(byte[] buffer, int offset, int count)
    {
        return output.Read(buffer, offset, count);
    }
    public override long Seek(long offset, SeekOrigin origin)
    {
        return output.Seek(offset, origin);
    }
    public override void SetLength(long value)
    {
        output.SetLength(value);
    }
    public override void Write(byte[] buffer, int offset, int count)
    {
        if (HttpContext.Current != null)
        {
            HttpContext context = HttpContext.Current;
            Encoding encoding = context.Response.ContentEncoding;
            string responseInfo = encoding.GetString(buffer, offset, count);
            responseInfo = responseInfo.Replace("\r\n", "");
            responseInfo = responseInfo.Substring(0, responseInfo.Length - 9);
            buffer = encoding.GetBytes(responseInfo);
            output.Write(buffer, 0, buffer.Length);
        }
    }
    #endregion
}

然后 在前台调用,已经可以直接返回不带xml的文本数据啦,这样就可以直接在webservice调用方法里面用Newtonsoft.Json.JsonConvert转化成JSON格式返回了。


转载自:http://www.qyuit.com/post/65.html

你可能感兴趣的:(html5,net)