我已經使用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格式字符串?
如果您遇到任何問題,只需瀏覽以查看-http://www.codeproject.com/Articles/167159/如何對創建-A-JSON-WCF的RESTful服務 - 在60秒 – vibhu