Khi cố tạo một dịch vụ đơn giản để trả về một chuỗi JSON đơn giản bằng cách làm theo một số hướng dẫn. Tôi bị kẹt trên hai máy khác nhau với yêu cầu HTTP Statuscode 400. Ví dụ hướng dẫn Dịch vụ WCF RESTful với JSON pt.1 & pt.2 - http://www.youtube.com/watch?v=5BbDxB_5CZ8C# 4.0 WCF REST JSON - HTTP GET CODE 400 Yêu cầu không hợp lệ
Tôi có còn Google và tìm kiếm ở đây (StackOverflow) cho vấn đề tương tự nhưng không thành công.
Vấn đề là tôi nhận được 400 yêu cầu xấu khi cố gắng thực hiện kiểm tra sanity để duyệt đến dịch vụ WCF và thực thi phương thức. Bằng cách biên soạn dịch vụ và duyệt qua địa chỉ này: http://localhost:49510/Service1.svc/GetPerson Giống như hướng dẫn. Tôi đã thử tìm một giải pháp trong 3 ngày. Bất kỳ trợ giúp được đánh giá cao.
Đây là những gì tôi làm.
Đầu tiên tôi tạo một dự án mới một ứng dụng Dịch vụ WCF đơn giản. Tôi xóa mặc định Service1.svc và thêm một dịch vụ WCF mới, tạo ra một Service1.svc mới và một IService1.cs
Đây là mã cho giao diện (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; }
}
}
đây là mã cho 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" };
}
}
}
và web.config là hoang sơ và tìm kiếm thích web.config này
<?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>
Tôi vừa sao chép và dán tệp như bạn đã nói! Bạn đã cứu ngày của tôi! Xuất sắc! Cảm ơn rất nhiều! – user1087261