3

I'm creating a skeleton algorithm of a restful service showing how to handle post and get requests. from my example get is working fine, however post does not. i guess i should add stuff to web.config, but i don't know what and why. thanks in advance, Zoli.

 [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; }
} 

web.config:

<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>

上記では、2つのエンドポイントが公開されます。1つはXMLデータを使用し、もう1つはJSONを使用します。もちろん、このように2つのエンドポイントを公開することは完全にオプションです。それはあなたができることのほんの一例です。

また、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 に答える