简体   繁体   English

让POST或PUT在WCF REST服务中工作

[英]Getting POST or PUT to work in a WCF REST Service

I have a WCF Service and I am able to GET from it but I can't work out how to PUT to it. 我有一个WCF服务,我可以从它获取,但我无法弄清楚如何PUT到它。 I just want to increment a hits field each time the record is used in the client. 我只是想在每次在客户端中使用记录时增加一个命中字段。 Here is some of my code. 这是我的一些代码。

Interface: 接口:

[OperationContract]
[WebInvoke(Method = "PUT",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "IncSMS")]
void IncSMS();

Method: 方法:

public void IncSMS()
{
    var business =
        (from p in _db.Businesses
            where p.BusinessID == 1
            select p).FirstOrDefault();
    business.SMSHits += 1;
    _db.SaveChanges();
}

I'm getting “Method not allowed.” In IE, can anyone see what I'm doing wrong??? 我得到“方法不允许。”在IE中,谁能看到我做错了什么?

Cheers, 干杯,

Mike. 麦克风。

Well, I asked the question and it took me three days to find the answer to something that should have been very simple. 好吧,我问了这个问题,花了三天时间才找到应该非常简单的答案。

So the question ended up being: How do I add a Content-Length header to my request??? 所以问题最终是:如何在我的请求中添加Content-Length标头?

Answer: You can't add Content-Length to a URI when typed into a browser, “because by default it will perform a GET”!!! 答:在键入浏览器时,不能将Content-Length添加到URI,“因为默认情况下它会执行GET”!!! To add Content-Length to your header you must use a debugging tool, eg. 要向头部添加Content-Length ,必须使用调试工具,例如。 Fiddler or build a form or some other type of client! 提琴手或建立表格或其他类型的客户!

In fiddler you simply type Content-Length : 0 in the “Request Headers” section of the “Request Builder” and it will magically work! 在fiddler中,您只需在“请求构建器”的“请求标题”部分中键入Content-Length :0,它就会神奇地工作! As seen near the bottom of this tutorial: http://blog.donnfelker.com/2008/12/04/how-to-rest-services-in-wcf-3-5-part-2-the-post/ 如本教程底部所示: http//blog.donnfelker.com/2008/12/04/how-to-rest-services-in-wcf-3-5-part-2-the-post/

Thanks guys, 多谢你们,

Mike. 麦克风。

Method not allowed generally means that, on the client side, you are using an HTTP method that does not match what you've configured in the WCF code. 不允许的方法通常意味着,在客户端,您使用的HTTP方法与您在WCF代码中配置的方法不匹配。 In your case you've specified PUT as the HTTP Method for that WCF -exposed service method. 在您的情况下,您已将PUT指定为该WCF暴露服务方法的HTTP方法。 It could be that you are using HTTP POST when your attribute says PUT. 当您的属性显示PUT时,可能是您正在使用HTTP POST。

If you use Fiddler, you will be able to see the request and response. 如果您使用Fiddler,您将能够看到请求和响应。 If the problem is as I've described, then when you send a POST, the raw response will look like this: 如果问题如我所述,那么当您发送POST时,原始响应将如下所示:

HTTP/1.1 405 Method Not Allowed
Allow: PUT
Content-Length: 1565
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 10 Nov 2011 02:17:43 GMT

The "Allow" header in the response will tell you: Please use PUT. 响应中的“允许”标题会告诉您:请使用PUT。

如果要发送PUT ,请尝试使用Fiddler ,这将允许您使用任何HTTP方法构建请求,修改请求标头,查看响应标头等。对于测试WCF服务非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM