告别复杂WCF扩展 REST过程 ,让他 so easy

      在看一些wcf的书和文章时,都会提到rest wcf ,实现方式多数通过对wcf进行一定程度的扩展实现的,实在是让我这种菜鸟心生畏惧~

前些天为了体验mvc3安装了vs2010,顺便在在线模板里面搜索了一下wcf,没想到有意外发现,有图为证,这年代无图无真相

微博桌面截图_20120101164415

选择wcf后,排在第一个的就是 wcf rest,左边的导航还有其他的一些在线的东西暂时没研究,不过vs2010真是个好东西,安装在线模板之后,新建项目

微博桌面截图_20120101164826

不知道为什么 不搜索一下wcf 还真的找不见安装的这个模板,新建项目之后,和网上大家介绍的rest wcf 差不多

我也小看了一下,首先是Global.asax

public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            // Edit the base address of Service1 by replacing the "Service1" string below
            RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));


        }
    }

看到这里下了一跳,以前没见过ASP.NET MVC的路由可以这样用,是在是不得不佩服 .NET4.0 里面路由功能的强大,

试运行一下,没想到 还有一个目录导航的功能,可以方便的看到那些接口可以调用,

http://localhost:4972/Service1/help

微博桌面截图_20120101165656

返回的数据时XML的,如何返回js或json格式的数据尚未研究,不过创建 wcf reset 真的是很方便

http://localhost:4972/Service1/

- <ArrayOfSampleItem xmlns="http://schemas.datacontract.org/2004/07/WcfRestService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
- <SampleItem>
  <Id>1</Id> 
  <StringValue>Hello</StringValue> 
  <obj i:nil="true" xmlns:a="http://schemas.datacontract.org/2004/07/System.ServiceModel.Syndication" /> 
  </SampleItem>
  </ArrayOfSampleItem>
test
原文地址:https://www.cnblogs.com/qqloving/p/2309529.html