2013-07-25 85 views
-1

我已經使用VS2012創建一個新的WCF網站(Add-> New Web Site-> WCF Service)。配置WCF WebSite以支持來自瀏覽器的RESTful訪問

「開箱即用」這給了我下面的web.config文件:

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5"/> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https"/> 
    </protocolMapping> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 
</configuration> 

這也給了我最初的服務類,如下所示:

public class Service : IService 
{ 
    public string GetData(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 

    public CompositeType GetDataUsingDataContract(CompositeType composite) 
    { 
     if (composite == null) 
     { 
      throw new ArgumentNullException("composite"); 
     } 
     if (composite.BoolValue) 
     { 
      composite.StringValue += "Suffix"; 
     } 
     return composite; 
    } 
} 

我再F5開始該項目啓動了一個瀏覽器窗口,其中列出了dir內容,包括Service.svc。

我想輸入一個URL來調用GetData方法。我在瀏覽器地址欄中輸入什麼內容,以及如何配置和/或修飾服務,以便我可以在瀏覽器中鍵入URL並查看返回的JSON格式字符串?

回答

1

馬克與下列屬性的操作在你的合同(IService接口) - [WebInvoke(方法= 「GET」,BodyStyle = WebMessageBodyStyle.WrappedRequest,ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,UriTemplate =「的GetData/{id}「)] 然後嘗試通過添加GetData/[任意數字]來從瀏覽器調用您的服務

+0

如果您遇到任何問題,只需瀏覽以查看-http://www.codeproject.com/Articles/167159/如何對創建-A-JSON-WCF的RESTful服務 - 在60秒 – vibhu