C#4.0 WCF REST JSON-HTTP GET CODE 400错误请求 [英] C# 4.0 WCF REST JSON - HTTP GET CODE 400 Bad Request
问题描述
在尝试创建简单服务以通过遵循一些教程来返回简单JSON字符串时.我在两台不同的计算机上遇到HTTP Statuscode 400错误的请求. 示例教程 带有JSON pt.1和amp;的RESTful WCF服务pt.2- http://www.youtube.com/watch?v=5BbDxB_5CZ8
When trying to create a simple service to return a simple JSON string by following several tutorials. I get stuck on two different machines with a HTTP Statuscode 400 bad request. Example tutorials RESTful WCF Service with JSON pt.1 & pt.2 - http://www.youtube.com/watch?v=5BbDxB_5CZ8
我也有Google,并在这里(StackOverflow)搜索了类似的问题,但没有成功.
I have also Google and searched here (StackOverflow) for similar problem without success.
问题是,当尝试进行健全性检查以浏览到WCF服务并执行该方法时,我收到400错误的请求.通过编译服务并浏览此地址: http://localhost:49510/Service1.svc/GetPerson 就像本教程一样.我尝试寻找解决方案已有3天.感谢您的帮助.
The problem is I get the 400 bad request when trying to do a sanity check to browse to the WCF service and execute the method. By compiling the service and browse this address: http://localhost:49510/Service1.svc/GetPerson Just like the tutorial. I have tried finding a solution for like 3 days. Any help is appreciated.
这就是我的工作.
首先,我创建一个简单的WCF Service应用程序的新项目.我删除默认的Service1.svc并添加一个新的WCF服务,该服务会生成一个新的Service1.svc和一个IService1.cs
First i create a new project a simple WCF Service application. I delete the default Service1.svc and add a new WCF Service, that generate a new Service1.svc and a IService1.cs
这是接口( IService1.cs )的代码
namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method="GET", BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate="GetPerson")]
Person GetPerson();
}
[DataContract(Name="Person")]
public class Person
{
[DataMember(Name="name")]
public string Name { get; set; }
}
}
这是 Service1.svc
namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
public Person GetPerson()
{
return new Person() { Name = "Tobbe" };
}
}
}
Web.config保持不变,看起来像这样的 web.config
And the Web.config is untouched and look likes this web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
推荐答案
对于REST WCF,您必须在web.config中进行绑定和端点设置
For REST WCF You have to do binding and endpoint setting in web.config
按照以下说明替换整个web.config,它将起作用
Replace your whole web.config by following and it will work
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
您还剩下2件事情
使用webHttpBinding(将默认的http端口映射更改为webHttpBinding)
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<behaviors>
<system.serviceModel>
指定webHttp端点行为
<system.serviceModel>
-----
</protocolMapping>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior >
</endpointBehaviors>
<behaviors>
------
<system.serviceModel>
这篇关于C#4.0 WCF REST JSON-HTTP GET CODE 400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!