为什么我通过 WCF Resful 服务在 "Put" 操作中收到“405 方法不允许”错误?
Why am I getting a "405 Method not allowed" error on "Put" operations through a WCF Resful service?
我有一个简单的 WCF 服务,它将 json 作为 input/output 参数。
它作为基于 SOAP 的服务正常工作,我得到了预期的结果。
我已将 RESTFul 个端点添加到服务中。
我正在使用 "Google-based" PostMan 来测试服务。
我可以在服务上成功调用 "Get" 方法和 return JSON 结果:
[OperationContract(IsOneWay = false)]
[WebGet(UriTemplate = "/Test/{calcID}",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetTestResults(string calcID);
当我尝试访问基于 "Put" 的方法时,我得到以下状态:
405 方法不允许
"Put" 方法的签名:
[OperationContract(IsOneWay = false)]
[WebInvoke(Method = "Post",
UriTemplate = "/GetCalculation",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetCalculation(CalcInput inputValues);
这是我在 PostMan 输出中看到的输出 window:
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Method not allowed.</p>
</div>
</body>
这是我的 RESTFul 接口端点的配置设置:
<system.serviceModel>
<services>
<service name="CalcServiceLibrary.CalcService">
<endpoint address="regular" binding="wsHttpBinding" name="wsHTTPEndPoints"
contract="CalcServiceLibrary.ICalcService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="json" binding="webHttpBinding" name="wsWebHTTPEndPoint"
contract="CalcServiceLibrary.ICalcServiceRESTful"
behaviorConfiguration="RESTfulBehavior" bindingConfiguration="crossDomain">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RESTfulBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
我确定我的配置定义中有些地方不太正确。
我已经研究了几篇文章,但未能提出解决方案。
新笔记::
我已将以下简单端点添加到我的服务中:
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string TestPost(string name)
{
return "Hello " + name + "!";
}
我正在使用 Postman 进行测试。这是我的 Postman 屏幕的屏幕截图:
这是我在输出中看到的:
感觉这一定是某种配置问题。
有人可以指出我做错了什么的正确方向吗?
看起来您指定 post 而不是 put 作为声明中的方法,也许更改为:
[OperationContract(IsOneWay = false)]
[WebInvoke(Method = "PUT",
UriTemplate = "/GetCalculation",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetCalculation(CalcInput inputValues);
在排除故障时,我尝试做的第一件事就是从基本要素入手。我建议删除 UriTemplate = "/GetCalculation"
和 BodyStyle = WebMessageBodyStyle.Bare)
属性参数。这就是我正在使用的:
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string TestPost(string name)
{
return "Hello " + name + "!";
}
编辑:
我想我应该看看 Postman,因为无论如何我都在做很多类似的工作。你能 post 一张你的 Postman 配置的截图吗?为了让 Postman 工作,我必须设置以下内容:
Type 设置为 POST(很明显)。但我还必须将 header Content-Type
设置为 application/json
.
为了发送数据,您不能使用 URL 参数(URL 字段旁边的 'Params' 按钮)。这仅在样式 ?name=bob
中插入,它适用于 GET
但不适用于 POST
。此外,我无法将数据指定为 JSON,因此我必须以 JSON 格式输入 raw
数据:
然后我得到一个有效的回复,Hello bob!
。
我有一个简单的 WCF 服务,它将 json 作为 input/output 参数。 它作为基于 SOAP 的服务正常工作,我得到了预期的结果。
我已将 RESTFul 个端点添加到服务中。 我正在使用 "Google-based" PostMan 来测试服务。
我可以在服务上成功调用 "Get" 方法和 return JSON 结果:
[OperationContract(IsOneWay = false)]
[WebGet(UriTemplate = "/Test/{calcID}",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetTestResults(string calcID);
当我尝试访问基于 "Put" 的方法时,我得到以下状态: 405 方法不允许
"Put" 方法的签名:
[OperationContract(IsOneWay = false)]
[WebInvoke(Method = "Post",
UriTemplate = "/GetCalculation",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetCalculation(CalcInput inputValues);
这是我在 PostMan 输出中看到的输出 window:
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Method not allowed.</p>
</div>
</body>
这是我的 RESTFul 接口端点的配置设置:
<system.serviceModel>
<services>
<service name="CalcServiceLibrary.CalcService">
<endpoint address="regular" binding="wsHttpBinding" name="wsHTTPEndPoints"
contract="CalcServiceLibrary.ICalcService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="json" binding="webHttpBinding" name="wsWebHTTPEndPoint"
contract="CalcServiceLibrary.ICalcServiceRESTful"
behaviorConfiguration="RESTfulBehavior" bindingConfiguration="crossDomain">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RESTfulBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
我确定我的配置定义中有些地方不太正确。 我已经研究了几篇文章,但未能提出解决方案。
新笔记:: 我已将以下简单端点添加到我的服务中:
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string TestPost(string name)
{
return "Hello " + name + "!";
}
我正在使用 Postman 进行测试。这是我的 Postman 屏幕的屏幕截图:
这是我在输出中看到的:
感觉这一定是某种配置问题。 有人可以指出我做错了什么的正确方向吗?
看起来您指定 post 而不是 put 作为声明中的方法,也许更改为:
[OperationContract(IsOneWay = false)]
[WebInvoke(Method = "PUT",
UriTemplate = "/GetCalculation",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetCalculation(CalcInput inputValues);
在排除故障时,我尝试做的第一件事就是从基本要素入手。我建议删除 UriTemplate = "/GetCalculation"
和 BodyStyle = WebMessageBodyStyle.Bare)
属性参数。这就是我正在使用的:
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string TestPost(string name)
{
return "Hello " + name + "!";
}
编辑: 我想我应该看看 Postman,因为无论如何我都在做很多类似的工作。你能 post 一张你的 Postman 配置的截图吗?为了让 Postman 工作,我必须设置以下内容:
Type 设置为 POST(很明显)。但我还必须将 header Content-Type
设置为 application/json
.
为了发送数据,您不能使用 URL 参数(URL 字段旁边的 'Params' 按钮)。这仅在样式 ?name=bob
中插入,它适用于 GET
但不适用于 POST
。此外,我无法将数据指定为 JSON,因此我必须以 JSON 格式输入 raw
数据:
然后我得到一个有效的回复,Hello bob!
。