C# WCF Web API 发布/放置问题

发布于 2024-11-17 07:42:55 字数 788 浏览 1 评论 0原文

我有一个看起来像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-11-24 07:42:56

解决方案 1.

您可以将 theTextToPut 参数更改为 HttpRequestMessage,然后读取消息的内容。

[WebInvoke(UriTemplate = "/{userName}/?key={key}&machineName={machineName}", Method = "PUT")]
public HttpResponseMessage<SomeStuffPutResponse> PutSomeStuff(string userName, string key, string machineName, HttpRequestMessage request)
{
     string theTextToPut = request.Content.ReadAsString();
}

解决方案 2。

如果您确实希望以字符串形式获取参数,则可以创建一个操作处理程序来处理名为“theTextToPut”的所有字符串参数。

public class TextToPutOperationHandler : HttpOperationHandler<HttpRequestMessage, string>
    {
        public TextToPutOperationHandler() 
            : this("theTextToPut")
        { }

        private TextToPutOperationHandler(string outputParameterName) 
            : base(outputParameterName)
        { }

        public override string OnHandle(HttpRequestMessage input)
        {
            return input.Content.ReadAsString();
        }
    }

然后您在 Global.asax 中设置您的服务,如下所示:

RouteTable.Routes.MapServiceRoute<SomeStuffService>("1.0/SomeStuff",
                new HttpHostConfiguration().AddRequestHandlers(x => x.Add(new TextToPutOperationHandler())));

Solution 1.

You could change the theTextToPut-parameter to a HttpRequestMessage and then read the content of the message.

[WebInvoke(UriTemplate = "/{userName}/?key={key}&machineName={machineName}", Method = "PUT")]
public HttpResponseMessage<SomeStuffPutResponse> PutSomeStuff(string userName, string key, string machineName, HttpRequestMessage request)
{
     string theTextToPut = request.Content.ReadAsString();
}

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

public class TextToPutOperationHandler : HttpOperationHandler<HttpRequestMessage, string>
    {
        public TextToPutOperationHandler() 
            : this("theTextToPut")
        { }

        private TextToPutOperationHandler(string outputParameterName) 
            : base(outputParameterName)
        { }

        public override string OnHandle(HttpRequestMessage input)
        {
            return input.Content.ReadAsString();
        }
    }

Then you set up your service in Global.asax as follows:

RouteTable.Routes.MapServiceRoute<SomeStuffService>("1.0/SomeStuff",
                new HttpHostConfiguration().AddRequestHandlers(x => x.Add(new TextToPutOperationHandler())));
丶视觉 2024-11-24 07:42:56

它正在 uri 中查找字符串 theTextToPut

It's looking for string theTextToPut in the uri.

晚雾 2024-11-24 07:42:56

正如 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文