locked
Reading data in my service send by "GET" method RRS feed

  • Question

  • User1935984934 posted

    I am using on a form a jquery which is sending to my service data this way

    http://localhost:49209/Services/Service1.svc/Getcity?q=data

    In my service I need to retrieve the data value. I have got my function, as shown below but I don't how to get the parameter send across. 

      <OperationContract()> _
        <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json)> _
        Public Function Getcity(ByVal fldCity As String) As String

    How do I get this data ?

    Tuesday, March 25, 2014 5:58 PM

Answers

  • User-417640953 posted

    Hi Corobori,

    Thank you post the issue to asp.net forum.

    Based on your description, I see you want to create a restfull service with .svc file. And pass some parameters to the webget operation.

    For this issue, please note below UriTemplates:

    [WebGet(UriTemplate = "Products/{name}")] 
    public Product GetProduct(string name) 
    // URL = "/InventoryService/Products/ProductXYZ" 
    
    
    [WebGet(UriTemplate = "Products/Product({name})")] 
    public Product GetProduct(string name) 
    // URL = "/InventoryService/Products/Product(ProductXYZ)" 
    
    
    [WebGet(UriTemplate = "Products/API.Service?Product={name}")] 
    public Product GetProduct(string name) 
    // URL = "/InventoryService/Products/API.Service?Product=ProductXYZ" 
    

    The UriTemplate can only use parameters of type string, Uri Templates are very flexible and you can build your own standard syntax for your api like above code.

    For more information, you can refer to below article. I think you will find your answer.

    http://www.zachhunter.com/2012/05/wcf-restful-services-part-2/

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 27, 2014 3:52 AM

All replies

  • User1208776063 posted

    Corobori

    but I don't how to get the parameter send across. 

    You can specify a UriTemplate to access the parameter value by name

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Getcity?q={city}")]        
    public void Getcity(string city)
    {
        //do somethign with the city here
    }

    I'm not an expert in VB. Please use online converter or add this manually in your code

    Tuesday, March 25, 2014 7:07 PM
  • User1935984934 posted

    dotnetzoom,

    It looks like it is somewhere around what you're saying but I can't find the correct syntax

    When using this 

        <OperationContract()> _
        <WebGet(UriTemplate:="Getcity?q={city}")> _
        Public Function Getcity(ByVal fldCity As String) As String

    I am getting the error shown below and the service isn't beeing fired.

    GET http://localhost:49209/Services/Service1.svc/Getcity?q=aaa 500 (Internal Server Error) jquery.js:2
    send jquery.js:2
    p.extend.ajax jquery.js:2
    p.(anonymous function) jquery.js:2
    p.extend.getJSON jquery.js:2
    (anonymous function)

    But if am using this

     <OperationContract()> _
        <WebGet()> _
        Public Function Getcity(ByVal fldCity As String) As String

    The service is fired but, of course, without template I can't have access to the parameter 

    Tuesday, March 25, 2014 8:39 PM
  • User1208776063 posted

        Public Function Getcity(ByVal fldCity As String) As String

    parameter name should be same. You need to change fldCity to city

    Public Function Getcity(ByVal city As String) As String

    Tuesday, March 25, 2014 8:41 PM
  • User1935984934 posted

    I tried this, and I am still getting 500 server error

        <OperationContract()> _
        <WebGet(ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/Getcity?q={city}")> _
        Public Function Getcity(ByVal city As String) As String

    Tuesday, March 25, 2014 8:59 PM
  • User-417640953 posted

    Hi Corobori,

    Thank you post the issue to asp.net forum.

    Based on your description, I see you want to create a restfull service with .svc file. And pass some parameters to the webget operation.

    For this issue, please note below UriTemplates:

    [WebGet(UriTemplate = "Products/{name}")] 
    public Product GetProduct(string name) 
    // URL = "/InventoryService/Products/ProductXYZ" 
    
    
    [WebGet(UriTemplate = "Products/Product({name})")] 
    public Product GetProduct(string name) 
    // URL = "/InventoryService/Products/Product(ProductXYZ)" 
    
    
    [WebGet(UriTemplate = "Products/API.Service?Product={name}")] 
    public Product GetProduct(string name) 
    // URL = "/InventoryService/Products/API.Service?Product=ProductXYZ" 
    

    The UriTemplate can only use parameters of type string, Uri Templates are very flexible and you can build your own standard syntax for your api like above code.

    For more information, you can refer to below article. I think you will find your answer.

    http://www.zachhunter.com/2012/05/wcf-restful-services-part-2/

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 27, 2014 3:52 AM