我正在创建一个宁静服务的框架算法,展示如何处理发布和获取请求。从我的示例中,get 工作正常,但是 post 没有。我想我应该在 web.config 中添加一些东西,但我不知道是什么以及为什么。在此先感谢,佐利。
[ServiceContract]
public interface IRestfulService
{
[OperationContract]
[WebGet(UriTemplate = "/GetAStudent")]
Student GetExistingStudent();
[OperationContract]
[WebInvoke(UriTemplate = "/GetTheGivenStudent/{studentName}", Method = "POST")]
Student GetGivenStudent(string studentName);
}
public class RestfulService : IRestfulService
{
public Student GetExistingStudent()
{
Student stdObj = new Student
{
StudentName = "Foo",
Age = 29,
Mark = 95
};
return stdObj;
}
public Student GetGivenStudent(string studentName)
{
Student stdObj = new Student
{
StudentName = studentName,
Age = 29,
Mark = 95
};
return stdObj;
}
}
[DataContract]
public class Student
{
[DataMember]
public string StudentName { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public double Mark { get; set; }
}
网络配置:
<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>