[英]WCF rest api - [WebGet] working, but endpoint not found for [WebInvoke(…GET…)]
[英]WCF rest WebInvoke get method not working returning 404 error
我使用默認方法創建了基本的WCF REST服務。 當我請求svc文件時,它正在工作,但是在放置帶有rest參數的請求時,它返回404錯誤。 也就是說,它給人的響應,當我打電話的http://localhost/FirstWCFRestApp/RestServiceImpl.svc但返回404錯誤,當我打電話的http://localhost/FirstWCFRestApp/RestServiceImpl.svc/xml/12 。
這是一種非常基本的服務,只有一種方法,使我感到困惑,因為它為什么不起作用。 我已經粘貼了下面的代碼。
請讓我知道問題出在哪里以及為什么它不起作用。
接口 `
using System.ServiceModel;
using System.ServiceModel.Web;
namespace FirstWCFRestApp
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServiceImpl" in both code and config file together.
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method="Get",UriTemplate="/xml/{id}",RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json)]
string DoWork(string id);
}
}
類文件
namespace FirstWCFRestApp
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RestServiceImpl" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select RestServiceImpl.svc or RestServiceImpl.svc.cs at the Solution Explorer and start debugging.
public class RestServiceImpl : IRestServiceImpl
{
public string DoWork(string id)
{
return "You requested Id is "+ id;
}
}
}
SVC文件
<%@ ServiceHost Language="C#" Debug="true" Service="FirstWCFRestApp.RestServiceImpl" CodeBehind="RestServiceImpl.svc.cs" %>
Web配置
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<behaviors>
<endpointBehaviors>
<behavior name="FWRBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="htBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="FirstWCFRestApp.RestServiceImpl" behaviorConfiguration="htBehaviour">
<endpoint address="Stud" binding="webHttpBinding"
contract="FirstWCFRestApp.IRestServiceImpl" behaviorConfiguration="FWRBehaviour"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
地址http://localhost/FirstWCFRestApp/RestServiceImpl.svc是服務元數據地址。 實際的服務地址應基於服務地址的UriTemplate屬性和Address屬性。
UriTemplate =“ / xml / {id}”
binding =“ webHttpBinding” contract =“ FirstWCFRestApp.IRestServiceImpl”> behaviorConfiguration =“ FWRBehaviour”>
此外,WebInvoke的Method屬性應大寫。
[WebInvoke(Method ="GET",ResponseFormat =WebMessageFormat.Json,UriTemplate ="/xml/{id}")]
總之,服務地址應為
http://localhost/FirstWCFRestApp/RestServiceImpl.svc/Stud/xml/12
請隨時告訴我是否有什么我可以幫助的。
就像亞伯拉罕所說:
Web調用格式: {SVC Path}/{webHttpBinding Endpoint Address}/{WebInvoke UriTemplate}
在您的情況下,應為:
http://localhost/FirstWCFRestApp/RestServiceImpl.svc/Stud/xml/12
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.