添加新建项目
在默认程序中
配置 web.config文件 配置 system.serviceModel
<services> <service name="s.Service1"> <endpoint binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="s.IService1" /> </service> </services> <behaviors> <!--WCF中提供了Web HTTP的方式--> <endpointBehaviors> <behavior name="WebBehavior"> <webHttp helpEnabled="true" /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="WebBehavior"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> <!--<serviceBehaviors> <behavior> --> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> <!-- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> --> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <!-- <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors>--> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
配置接口允许跨域
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />
</customHeaders>
</httpProtocol>
最后接口定义中 iservice默认post
添加配置文件
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]