C# WCF Web API 发布/放置问题
我有一个看起来像这样的 Web 服务:
[WebInvoke(UriTemplate = "/{userName}/?key={key}&machineName={machineName}", Method = "PUT")]
public HttpResponseMessage<SomeStuffPutResponse> PutSomeStuff(string userName, string key, string machineName, string theTextToPut)
{
// do stuff
}
我的 global.asx 看起来像:
RouteTable.Routes.MapServiceRoute<SomeStuffService>("1.0/SomeStuff", new HttpHostConfiguration());
当我通过 C# HttpClient 或 fiddler 访问 Web 服务时,它会抛出 500,甚至无法访问我的方法。我添加了一堆日志记录并收到以下错误:
服务操作 “PutSomeStuff”需要一个值 可指定为输入类型“String” 参数'requestMessage'但是 收到一个类型的值 'HttpRequestMessage`1'。
更新:如果我将 TextToPut 变量设为自定义对象,它就可以正常工作。如果它是像字符串这样的原始类型,它只会给我带来问题。
I have a webservice that looks something like this:
[WebInvoke(UriTemplate = "/{userName}/?key={key}&machineName={machineName}", Method = "PUT")]
public HttpResponseMessage<SomeStuffPutResponse> PutSomeStuff(string userName, string key, string machineName, string theTextToPut)
{
// do stuff
}
My global.asx looks like:
RouteTable.Routes.MapServiceRoute<SomeStuffService>("1.0/SomeStuff", new HttpHostConfiguration());
When I hit the webservice via C# HttpClient or fiddler it is throwing a 500 and not even getting to my method. I added a bunch of logging and am getting the following error:
The service operation
'PutSomeStuff' expected a value
assignable to type 'String' for input
parameter 'requestMessage' but
received a value of type
'HttpRequestMessage`1'.
UPDATE: If I make theTextToPut variable a custom object it works fine. It is just giving me issues if it is a primitive type like a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案 1.
您可以将 theTextToPut 参数更改为 HttpRequestMessage,然后读取消息的内容。
解决方案 2。
如果您确实希望以字符串形式获取参数,则可以创建一个操作处理程序来处理名为“theTextToPut”的所有字符串参数。
然后您在 Global.asax 中设置您的服务,如下所示:
Solution 1.
You could change the theTextToPut-parameter to a HttpRequestMessage and then read the content of the message.
Solution 2.
If you really want to get the parameter as a string you could create a operation handler that handles all string parameters named "theTextToPut".
Then you set up your service in Global.asax as follows:
它正在 uri 中查找字符串
theTextToPut
。It's looking for string
theTextToPut
in the uri.正如 @axel22 所说,应用程序可能正在将
theTextToPut
绑定到 URI。正如这篇文章状态,简单类型默认绑定到 URI。您可以使用 FromBody 属性< /a> 强制应用程序将
theTextToPut
绑定到请求正文。As @axel22 says, probably the application is binding
theTextToPut
to the URI. As this article states, simple types are binded to the URI by default.You could use FromBody attribute to force the application to bind
theTextToPut
to the request body.