none
GET calls with a REST service vs RPC RRS feed

  • Question

  • Hi

    I am used to RPC and am trying to get my head wrapped around the GET call using REST with a webApi.  If we assume at a high level that the object in question is a book

    public class Book

    {

    public int Id {get;set;}

    public string Title {get; set;}

    public string Author {get;set;}

    public string Publisher {get;set;}

    public int Year {get;set;}

    }

    The GET for returning all is straightforward but GET/ID is unclear to me.  If ID isn't really the useful field and perhaps the user might want to search on any of the other Properties (Title, Author, Publisher, Year) or even a subset of them.  How do you model the Controller GET and make it RESTful?  

    Thank you.

    Robin

    Thursday, August 24, 2017 2:22 PM

All replies

  • It's where you can post in the future.

    https://forums.asp.net/

    using System.Collections.Generic;
    using System.Web.Http;
    using Entities;
    using Helper.Enums;
    using ServiceLayer;
    
    namespace WebAPI.Controllers
    {
        public class StudentController : ApiController, IStudentControllerAPI
        {
            private IServiceA srv;
            public StudentController(IServiceA service)
            {
                srv = service;
            }
    
            [HttpGet]
            public List<DTOStudent> GetStudents()
            {
                var dtos = srv.GetStudents();
    
                return dtos;
            }
    
            [HttpGet]
            public DTOStudent GetStudentById(int id)
            {
                return srv.GetStudentById(id);
            }
    
            [HttpPost]
            public void CreateStudent(DTOStudent dto)
            {
                srv.CreateStudent(dto);
            }
    
            [HttpPost]
            public void UpdateStudent(DTOStudent dto)
            {
                srv.UpdateStudent(dto);
            }
    
            [HttpPost]
            public void DeleteStudent(int id)
            {
                srv.DeleteStudent(id);
            }
    
            [HttpGet]
            public bool IsStudentAuthorised(ResourceTypeEnum resourceType, object resourceId, string username)
            {
                return Models.Helper.IsStudentAuthorised(resourceType, resourceId, username, srv);
            }
        }
    }
    


    Thursday, August 24, 2017 4:17 PM
  • Thank you for the response.  I think you missed my question though.  To use your example, what if I wanted to allow to get a DTOStudent object returned based on the students first or last name or address or email?  Maybe a combination of some of them?  If I was using RPC I could pass the model back and investigate the properties or even make a GetByFirstName, GetByLastName, GetByEmail etc.  However if I look at the REST examples of services, Student would have 2 Gets, a POST, PUT, and DELETE for it and the controller should know what to do with the same uri (except of course get by id).  

    Thanks,

    Robin


    Thursday, August 24, 2017 4:36 PM
  • In the REST world you'd expose a Get action that allows the user to specify the criteria they want to search on. You'd then have to handle the various combinations. Note that OData actually solves this problem so you could look at their spec.

    REST is all about URLs so that is what I'd focus on. In this example I'm assuming the resource's url is <url>/v1/books. Resources are plural in REST, generally.

    GET <url>/v1/books - Get all books
    GET <url>/v1/books?title=Harry%20Potter&year=2004 - Get all books called/like Harry Potter with a year of 2004
    GET <url>/v1/books/10 - Get the book with ID 10

    Of course each of these you have to implement. For the query you would generally create a model that lists all the possible query fields. Then in the controller you'd need to convert that to a real query. The "get all" version would basically be the same action but with no filtering. What you expose as filtering options is completely up to you. Additionally sorting and paging are generally built into the query strings.

    For the ID version you don't necessarily need one. Depends upon the API. Additionally you may use anything other than an integral ID. You could use an ISBN, guid or even the book title. It depends upon what kind of API you want to build out.

    As for the POST, PUT, DELETE. You only need those if you are going to allow modifications to the resource. For example if you want to allow a user to update a book then POST or PUT would make sense. The body of the request would contain the updated book information.

    POST (or PUT) <url>/v1/books/{id} (JSON body is the book data) - Updates a book
    POST <url>/v1/books (JSON body is the book data) - Adds a new book

    If you wanted to allow a user to delete a book then exposing a resource for that would make sense.

    DELETE <url>/v1/books/{id} - Deletes the book with the given ID.

    Ultimately you need to decide what resources will be exposed by your API and what "actions" each resource will support. There are many, many resources online available to discuss the conventions used for this in REST.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Thursday, August 24, 2017 5:33 PM
  • You said WebAPI which yes is restful,  and you can follow the get/put pattern. Just because you can follow the pattern explicitly with WebApi, it doesn't mean that you have to.

    Instead of using the naming convention for HTTP methods, you can explicitly specify the HTTP method for an action by decorating the action method with the HttpGet, HttpPut, HttpPost, or HttpDelete attribute.

     [HttpGet]
     public List<DTOStudent> GetStudents()
     {
         var dtos = srv.GetStudents();
    
         return dtos;
     }
    

    [HttpGet]

    public List<DTOStudent> GetStudentsByIdAndName(int id, string name)

    [HttpGet]

    public List<DTOStudent> GetStudentsByIdAndName().Where(x => x.id == id, x.Name == name)

    Thursday, August 24, 2017 6:08 PM
  • Hi Robin,

    According to your question is more related to web api, you could post a new thread  to web api forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools . If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution .

    Best Regards,
    feih-7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 29, 2017 10:08 AM
  • Pardon my denseness but how does the IsStudentAuthorized action fit in with the REST model?  The ones above are consistent with the examples I have found but it looks like an RPC call to me in as much as a I cannot call www.yourdomain.com/api/student and get a response from it.  I would think that this would return a List<DTOStudent>

    Thanks.

    Wednesday, September 6, 2017 8:45 PM