WCF REST 4.0 PUT/POST/DELETE不起作用(400错误请求)
我一直在设计.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"
你是否在正确设置Content-Type
头的请求?对于XML请求应该是text/xml
,对于JSON应该是application/json
。当我在Fiddler中设置Content-Type时,您的代码适用于我。
你也应该设置Accept
头在你的GET要么text/xml
或application/json
这取决于你想在响应的格式。这是正常的POST作为服务器会认为你想在同一响应格式作为请求,因为您在web.config中设置了automaticFormatSelectionEnabled="true"
。有关WCF REST格式选择的更多详细信息,请参阅:http://blogs.msdn.com/b/endpoint/archive/2010/01/18/automatic-and-explicit-format-selection-in-wcf-webhttp-services.aspx
您的属性不应该在实施中,它们应该在操作合同中。您还需要确保您拥有UriTemplate中包含的任何命名参数。它们区分大小写,因此它们必须完全匹配。
IService.cs
[ServiceContract]
public class IService1
{
[WebGet(UriTemplate = "")]
[OperationContract]
public string HelloWorld();
[WebInvoke(UriTemplate = "/{name}", Method = "POST")]
[OperationContract]
public string HelloWorldPost(string name);
}
Service.cs
public class Service1 : IService
{
public string HelloWorld()
{
return "hello world";
}
public string HelloWorldPost(string name)
{
return "hello " + name;
}
}
你需要在你的web.config文件配置服务以及System.ServiceModel
下<system.serviceModel>
<services>
<service name="Service1">
<endpoint address="basic" binding="basicHttpBinding" contract="IService1" />
</service>
<services>
</system.serviceModel>
这是一些主要概念,应该让你开始朝正确的方向发展。如果你想要一个好的测试项目,只需在VS2010中使用“WCF Application”项目模板即可。它包含了大部分所需的部件。希望这可以帮助!
@为什么“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