3

我正在创建一个宁静服务的框架算法,展示如何处理发布和获取请求。从我的示例中,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>

4

1 回答 1

0

您不需要为 REST 服务公开 mex 端点。您的 web.config 应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="BookService">

        <!-- Expose an XML endpoint: -->
        <endpoint name="xml"
              address="xml"
              binding="webHttpBinding"
              contract="BookStore.Contracts.IBookService"
              behaviorConfiguration="poxBehavior" />

        <!-- Expose a JSON endpoint: -->
        <endpoint name="json"
              address="json"
              binding="webHttpBinding"
              contract="BookStore.Contracts.IBookService"
              behaviorConfiguration="jsonBehavior" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="poxBehavior">
           <webHttp />
        </behavior>
      <endpointBehaviors>
        <behavior name="jsonBehavior">
           <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

以上将公开两个端点,一个使用 XML 数据,一个使用 JSON。当然,像这样暴露两个端点是完全可选的;这只是你可以做的一个例子。

我也喜欢为 REST 服务使用路由;类似于您的 Global.asax.cs 中的内容:

protected void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.Add(
        new System.ServiceModel.Activation.ServiceRoute("books",
            new System.ServiceModel.Activation.WebServiceHostFactory(),
            typeof(BookStore.Services.BookService)
        )
    );
}

其中,使用示例 web.config 中的上述端点,将允许像这样访问服务:

http://yourdomain.com/books/xml

如果您选择使用或添加 json 端点,如下所示:

http://yourdomain.com/books/json
于 2012-04-12T15:24:02.070 回答