gpt4 book ai didi

c# - 为什么我通过 WCF Resful 服务在 "405 Method not allowed"操作上收到 "Put"错误?

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:30 24 4
gpt4 key购买 nike

我有一个简单的 WCF 服务,它将 json 作为输入/输出参数。它作为基于 SOAP 的服务正常工作,我得到了预期的结果。

我已将 RESTFul 端点添加到服务中。我正在使用“基于 Google 的”PostMan 来测试该服务。

我可以在服务上成功调用“Get”方法并返回 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 输出窗口中看到的输出:

<body>
<div id="content">
<p class="heading1">Service</p>
<p>Method not allowed.</p>
</div>
</body>

这是我的 RESTFul 接口(interface)端点配置设置:

<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 屏幕的屏幕截图: Postman Input Screenshot

这是我在输出中看到的: Postman Output Screenshot

这感觉像是某种配置问题。有人可以指出我做错了什么的正确方向吗?

最佳答案

看起来你正在指定 post 而不是 put 作为声明中的方法,也许更改为:

[OperationContract(IsOneWay = false)]
[WebInvoke(Method = "PUT",
UriTemplate = "/GetCalculation",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetCalculation(CalcInput inputValues);

关于c# - 为什么我通过 WCF Resful 服务在 "405 Method not allowed"操作上收到 "Put"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com