locked
One URITemplate for More Than One Method RRS feed

  • Question

  • Hi,

    I am new on REST and want to build some flexible RESTful WCF Services.

    I want to use my base address as the URITemplate for all of method define in that .svc file.

    But it is not allowing me to get the same URi for more than one method.

    Is it the condition that we have to use the unique URi fro each and every Method.

    I am Passing different parameter in the all method so it should Identify at one URL.

    Thanks


    Kumar Gaurav
    Monday, October 25, 2010 6:56 AM

Answers

  • No. You code will not work.

    WebInvoke always have a Method attribute. If you omit Method, default method is "POST". That is, you have two operations with identical UriTempalte & Method, this is not allowed.

    Following declaration looks more reasonable.  (assume you have declared a type named Customer)

    [WebInvoke(UriTemplate = "", Method = "POST")]
    Public int AddCustomer(Customer customerInfo);

    [WebGet(UriTemplate="")]
    Public Customer GetCustomer(int CustomerId)

    I strongly recommend the two articles in my previous reply:) they are step by step, pretty easy to follow. That will save you a lot of time learning WCF RESTful service.

    Besides, if you are using .Net 4.0 to implement your REST service, you may consider adding following section to your Web.Config. By doing this, you will get a Help page at http://YourSvcAddress/Help .

      <system.serviceModel>
        <standardEndpoints>
          <webHttpEndpoint>
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
          </webHttpEndpoint>
        </standardEndpoints>
      </system.serviceModel>

    • Marked as answer by kgaurav1023 Tuesday, October 26, 2010 5:04 AM
    Tuesday, October 26, 2010 1:49 AM

All replies

  • You probably made quite a few mistakes:) Why not paste your ServiceContract code here. People should be able to help better.

    A few points
    1. UriTemplate specifies the  relative address to your service base address. So you probably should not use base address as tempalte. You can just just specify the URI template as empty string "" so that the operation's address is just the .svc base address.

    2. Operations of a REST service are differentiated by URITempalte & Method. Two operation with same URITemplate are allowed, as long as their Method is different. E.g, one is "Post", another is "Delete". Please note that passing different parameters does not help differentiate WCF WebHttp resources.

    Following are two pretty nice articles on starting with WCF RESTFul services.
    http://blogs.msdn.com/b/endpoint/archive/2010/01/06/introducing-wcf-webhttp-services-in-net-4.aspx
    http://msdn.microsoft.com/en-us/library/bb412169.aspx

    Monday, October 25, 2010 7:48 AM
  • Hi Lei ,

    Thanks for answer.

    I just want to confirm That will the below lines will work or not .. i.e.

    [WebInvoke(UriTemplate="")]

    Public int AddCustomer()

     

    [WebInvoke(UriTemplate="")]

    Public int GetCustomer()

    because i want to use my base serivce to call all the methods which define within the Operations .

    Because it is very difficult to give different address for each method (even we can use different method to use same address).

    Thanks

     


    Kumar Gaurav
    Monday, October 25, 2010 1:32 PM
  • No. You code will not work.

    WebInvoke always have a Method attribute. If you omit Method, default method is "POST". That is, you have two operations with identical UriTempalte & Method, this is not allowed.

    Following declaration looks more reasonable.  (assume you have declared a type named Customer)

    [WebInvoke(UriTemplate = "", Method = "POST")]
    Public int AddCustomer(Customer customerInfo);

    [WebGet(UriTemplate="")]
    Public Customer GetCustomer(int CustomerId)

    I strongly recommend the two articles in my previous reply:) they are step by step, pretty easy to follow. That will save you a lot of time learning WCF RESTful service.

    Besides, if you are using .Net 4.0 to implement your REST service, you may consider adding following section to your Web.Config. By doing this, you will get a Help page at http://YourSvcAddress/Help .

      <system.serviceModel>
        <standardEndpoints>
          <webHttpEndpoint>
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
          </webHttpEndpoint>
        </standardEndpoints>
      </system.serviceModel>

    • Marked as answer by kgaurav1023 Tuesday, October 26, 2010 5:04 AM
    Tuesday, October 26, 2010 1:49 AM