【发布时间】:2011-03-30 18:33:10
【问题描述】:
我正在尝试通过 wcf 创建 REST 服务并已成功实现通过 [WebInvoke(Method = "GET")] 调用的功能
现在我想创建一个使用Method="POST" 的更新函数。这失败了 405: Method not allowed。我怀疑我可能需要在我的 web.config 中配置一些东西。
我在 VS2010 调试器中运行我的 wcf 服务时收到此错误。
这是服务的定义:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "object/{id}?status={status}&reason={reason}")]
Textblock SetObjectStatus(string id, string status, string reason);
当我通过 HttpWebRequest req 和 Method = "POST" 调用此方法时,我收到错误 405: Method not allowed。
我的 web.config 如下所示:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="WcfService1.TextblockService">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService1.ITextblockService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
【问题讨论】:
-
您还发送哪些请求?
-
我使用的 URL 是:localhost:54270/Service1.svc/object/1 用于 GET 请求,localhost:54270/Service1.svc/object 用于 POST 请求。
-
@SecretDeveloper:谢谢。你的问题引发了另一个新的面貌,现在我发现了问题。