我一直在設計.NET 4.0中的WCF REST服務。我的GET請求正常工作,但涉及將數據發佈到服務器的任何請求都失敗,出現HTTP 400 Bad Request
。WCF REST 4.0 PUT/POST/DELETE不起作用(400錯誤請求)
這是我的簡單的服務:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{
[WebGet(UriTemplate = "")]
public string HelloWorld()
{
return "hello world";
}
[WebInvoke(UriTemplate = "", Method = "POST")]
public string HelloWorldPost(string name)
{
return "hello " + name;
}
}
我的web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<protocolMapping>
<add scheme="http" binding="webHttpBinding" />
</protocolMapping>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
而且我的Global.asax:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
}
}
基本上,一切都是從模板默認,但我剛剛簡化了Service1
。我已經嘗試通過調試器運行它並通過Fiddler傳遞請求並在IIS中運行它並執行相同的操作,以及使用簡單的控制檯應用程序來僞造POST,但我總是得到400 Bad Request
錯誤,我不知道爲什麼。我已經看遍了整個互聯網,無法找到任何東西。
我試圖同時滿足以下兩個要求的例子(沒有工作):
XML:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string>
JSON:
"String content"
@爲什麼「rest」標籤被移除,這篇文章是關於REST網絡服務的...... – shuniar 2011-12-16 16:16:22
這篇文章是關於HTTP,WCF,ASP,.NET等的問題。它與REST架構。這是一個實現問題,而不是設計或體系結構問題。 – 2011-12-16 16:56:35
HelloWorld的GET版本是否工作? – 2011-12-18 18:31:05