zoukankan      html  css  js  c++  java
  • WCF返回JSON的详细配置

    开发环境:VS2008,c#

    1.新建个WCF服务网站

    文件-新建-网站-WCF服务

    2,运行一下,提示配置WEB.CONFIG,点击确认.

    3,打开web.config增加如下节点:

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
      </serviceHostingEnvironment>

     endpoint 中增加 behaviorConfiguration="webBehavior"

    <endpointBehaviors>
        <behavior name="webBehavior">
         <webHttp/>
        </behavior>
       </endpointBehaviors>

    处理完以上3处之后,web.config就OK了.

    4,IService.cs 增加:

    [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        List<CompositeType> Test();

    Service.cs 增加:

    public List<CompositeType> Test()
        {
            List<CompositeType> lst = new List<CompositeType>();
            CompositeType type = new CompositeType();
            type.BoolValue = true;
            type.StringValue = "22";
            lst.Add(type);
            CompositeType type2 = new CompositeType();
            type2.BoolValue = false;
            type2.StringValue = "33";
            lst.Add(type2);
            return lst;
        }

    CompositeType类:

    [DataContract]

    public class CompositeType

    {     bool boolValue = true;     string stringValue = "Hello ";

        [DataMember]    

    public bool BoolValue     {         get { return boolValue; }         set { boolValue = value; }     }

        [DataMember]    

    public string StringValue     {         get { return stringValue; }         set { stringValue = value; }     }

    }

    5,Service.cs 增加

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    6,运行测试,如:

    http://localhost:1177/WCFData/Service.svc

    手动输入:http://localhost:1177/WCFData/Service.svc/Test

    返回:[{"BoolValue":true,"StringValue":"22"},{"BoolValue":false,"StringValue":"33"}]

    测试成功!

  • 相关阅读:
    @Autowired和@Resource的区别是什么?
    关于事务,事务的特性,spring事务的传播特性
    Java 用Freemarker完美导出word文档(带图片)
    关于 MySQL查询当天、本周,本月,上一个月的数据
    js如何使用radio
    Freemarker提供了3种加载模板目录的方法
    190707Python-MySQL
    190707Python-RabbitMQ
    190707select和selector模块
    4、kubernetes资源清单快速入门190625
  • 原文地址:https://www.cnblogs.com/gaibangdaxia/p/5730382.html
Copyright © 2011-2022 走看看