构建Restful风格的WCF

本文详细介绍了如何使用Visual Studio 2017创建并配置WCF服务,包括添加Restful支持的过程。通过修改Web.config文件和添加全局应用程序类,实现了服务的RESTful接口,并提供了查看服务帮助文档的方法。

1, 新增默认的WCF项目

用VS2017新增WCF项目, 新增项目后应该直接可以运行起来
运行起来 打开类似这个目录, 应该可以看到Service1服务的帮助文档 http://localhost:52217/Service1.svc
并可打开WebService服务: http://localhost:52217/Service1.svc?wsdl
在这里插入图片描述

2, 添加Restful支持: 配置Web.config

在Web.config的<system.serviceModel>节点内添加如下代码

    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
              Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
              via the attributes on the <standardEndpoint> element below
          -->
        <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" />
      </webHttpEndpoint>
    </standardEndpoints>

3, 添加Restful支持: 添加Global类

右键项目–>添加–>全局应用程序类在这里插入图片描述
在Global 类中加上服务的路由

namespace WcfService1
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
        }
    }
}

完成了
运行项目, 可以打开服务帮助页面了, 类似 http://localhost:52217/Service1/help

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值