WCF rest WebInvoke get 方法不起作用,返回 404 错误



我使用默认方法创建了一个基本的WCF REST服务。 当我请求 svc 文件时它正在工作,但在使用 rest 参数发出请求时返回 404 错误。 即当我调用 http://localhost/FirstWCFRestApp/RestServiceImpl.svc 时它会给出响应,但在我调用 http://localhost/FirstWCFRestApp/RestServiceImpl.svc/xml/12 时返回 404 错误。

这是非常基本的服务,只有一种方法,让我困惑为什么它不起作用。 我已经粘贴了下面的代码。

请让我知道哪里出了问题以及为什么它不起作用。

界面'

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" %>

网络配置

<?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 属性和地址属性。

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

最新更新