从 WCF Web API 接收 404 错误(Put 操作)
我使用 WCF Web API 创建了一个休息服务。我的 GET 请求工作得很好,但我似乎无法让 PUT 请求工作。我不断收到 404 错误,就像资源不存在一样,但它确实存在。
服务器代码:
<WebInvoke(UriTemplate:="WebMaint", Method:="Put")>
Public Function WebMaintPut(ByVal entries As HttpRequestMessage(Of IList(Of WebMaint))) As HttpResponseMessage(Of WebMaint)
Dim response As New HttpResponseMessage(Of WebMaint)(HttpStatusCode.NoContent)
Using scope = New TransactionScope()
entries.Content.ReadAs(Of IList(Of WebMaint)).AsParallel().ForAll(Sub(entry)
_repos.Update(entry)
End Sub)
scope.Complete()
End Using
Return response
End Function
主机代码:
Public Sub New()
ObjectFactory.Initialize(Sub(x)
x.For(Of IWebMaintRepository)().Use(Of WebMaintRepository).Ctor(Of String).Is(ConfigurationManager.ConnectionStrings("Dev").ConnectionString)
End Sub)
_host = New HttpServiceHost(ObjectFactory.GetInstance(Of WebMaintResource), "http://localhost:8000/REST/")
_host.Open()
End Sub
客户端代码:
Private Sub btnSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSave.Click
Using client = New HttpClient()
Dim Content = New ObjectContent(Of IList(Of WebMaint))(_WebMaint)
Dim response = client.Put(New Uri("http://localhost:8000/REST/WebMaint/"), Content)
Debug.Print(response.StatusCode.ToString())
End Using
End Sub
PUT 请求:
PUT http://localhost:8000/REST/WebMaint/ HTTP/1.1
Host: localhost:8000
Content-Length: 592
Expect: 100-continue
<?xml version="1.0" encoding="utf-8"?><ArrayOfWebMaint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><WebMaint><WebMaintenanceID>1</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>1</WebSystemId><WebMsgId>4</WebMsgId></WebMaint><WebMaint><WebMaintenanceID>2</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>2</WebSystemId><WebMsgId>3</WebMsgId></WebMaint><WebMaint><WebMaintenanceID>3</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>3</WebSystemId><WebMsgId>2</WebMsgId></WebMaint></ArrayOfWebMaint>
PUT 响应:
HTTP/1.1 404 Not Found
Content-Length: 1565
Content-Type: text/html; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 16 Nov 2011 19:31:43 GMT
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Service</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Endpoint not found.</p>
</div>
</body>
</html>
I created a rest svc using WCF Web API. My GET Requests work just fine but i can't seem to get PUT requests to work. I keep getting 404 errors like the resource isn't there but it is.
Server Code:
<WebInvoke(UriTemplate:="WebMaint", Method:="Put")>
Public Function WebMaintPut(ByVal entries As HttpRequestMessage(Of IList(Of WebMaint))) As HttpResponseMessage(Of WebMaint)
Dim response As New HttpResponseMessage(Of WebMaint)(HttpStatusCode.NoContent)
Using scope = New TransactionScope()
entries.Content.ReadAs(Of IList(Of WebMaint)).AsParallel().ForAll(Sub(entry)
_repos.Update(entry)
End Sub)
scope.Complete()
End Using
Return response
End Function
Host Code:
Public Sub New()
ObjectFactory.Initialize(Sub(x)
x.For(Of IWebMaintRepository)().Use(Of WebMaintRepository).Ctor(Of String).Is(ConfigurationManager.ConnectionStrings("Dev").ConnectionString)
End Sub)
_host = New HttpServiceHost(ObjectFactory.GetInstance(Of WebMaintResource), "http://localhost:8000/REST/")
_host.Open()
End Sub
Client Code:
Private Sub btnSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSave.Click
Using client = New HttpClient()
Dim Content = New ObjectContent(Of IList(Of WebMaint))(_WebMaint)
Dim response = client.Put(New Uri("http://localhost:8000/REST/WebMaint/"), Content)
Debug.Print(response.StatusCode.ToString())
End Using
End Sub
PUT Request:
PUT http://localhost:8000/REST/WebMaint/ HTTP/1.1
Host: localhost:8000
Content-Length: 592
Expect: 100-continue
<?xml version="1.0" encoding="utf-8"?><ArrayOfWebMaint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><WebMaint><WebMaintenanceID>1</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>1</WebSystemId><WebMsgId>4</WebMsgId></WebMaint><WebMaint><WebMaintenanceID>2</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>2</WebSystemId><WebMsgId>3</WebMsgId></WebMaint><WebMaint><WebMaintenanceID>3</WebMaintenanceID><Maintenance>true</Maintenance><WebSystemId>3</WebSystemId><WebMsgId>2</WebMsgId></WebMaint></ArrayOfWebMaint>
PUT Response:
HTTP/1.1 404 Not Found
Content-Length: 1565
Content-Type: text/html; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 16 Nov 2011 19:31:43 GMT
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Service</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Endpoint not found.</p>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道,但您是否也尝试过 PUT 而不是 Put 放入 WebInvoke 属性中? (听起来很尴尬,但它是预览版,所以可能是可能的)
您尝试使用 List 而不是 IList 吗?
另一种可能性是您可能需要将请求中的 Content-Type 显式设置为 text/xml。
如果您在 IIS (Express) 上托管,则需要 启用 PUT 和 DELETE,因为默认情况下它们是禁用的。
I dunno, but did you try also PUT instead of Put in the WebInvoke Attribute? (Sounds awkward but it's a Preview so it might be possible)
Did you try using List instead of IList?
Another possiblity is you may need to set the Content-Type in your request to text/xml explicitily.
If you're hosting on IIS (Express) you need to enable PUT and DELETE as they're disabled by default.
我发现了问题。问题出在下面一行:
不管你相信与否,“method”参数区分大小写。将其更改为:
修复了问题。我认为应该有一个比仅仅收到 http 404 错误更好的解决方案。I found the problem. The problem was in the following line:
Believe it or not the "method" parameter is case sensitive. Changing it to :
<WebInvoke(UriTemplate:="WebMaint", Method:="PUT")>
fixed the problem. I think there should be a better solution than just to receive an http 404 error.