Getting Started - Web API


ASP.NET Web API

    • After created data model in Library project - From solution explorer right click on your project  , select Add, then select new Scaffolded Item , select Web API Controller - Empty, In the Add Controller dialog name the controller BooksController (Web API controllers inherit the ApiController class that handles HTTP requests).
    • Add new folder named ViewModels , In ViewModels folder add new class Named BooksVm and class named AuthorVm Write the following :
    "You can consider using of viewModels classes as data transfer object(DTO)
    this is to avoid circular references becouese of navagtion property in Author class
    also you may need to hide or add some new property. see also"

    //BookVm.cs


    namespace Library.ViewModels
    {
        public class BookVm
        {
            public int BookId { get; set; }
            public string BookName { get; set; }
            public int AuthorId { get; set; }
            public string AuthorName { get; set; }
        }

    }

    //AuthorVm.cs


    namespace Library.ViewModels
    {
        public class AuthorVm
        {
            public int AuthorId{ get; set; }
            public string AuthorName { get; set; }
            public int BookCount { get; set; }
        }

    }

    • Back to Controllers ,ِRight click in Controllers folder and  Add new Web API Controller - Empty, In the Add Controller dialog name the controller AuthorsController
    • Add the following methods(These methods implement GET , Post request) to BooksController.cs and AuthorsController.cs
    //BooksController.cs


    namespace Library.Controllers
    {
        [Route("api/books")]
        public class BooksController : ApiController
        {
            LibraryDbContext db = new LibraryDbContext();
            public IQueryable<BookVm> GetBooks()
            {
                var books = from book in db.Books
                           select new BookVm
                           {
                               BookId = book.BookId,
                               BookName = book.BookName,
                               AuthorId = book.AuthorId,
                               AuthorName = book.Author.AuthorName
                           };
                return books;
            }
        }
    }

    //AuthorController.cs


    namespace Library.Controllers
    {
        [Route("api/authors")]
        public class AuthorsController : ApiController
        {
            LibraryDbContext db = new LibraryDbContext();
            public IQueryable<AuthorVm> GetBooks()
            {
                var authors = from author in db.Authors
                           select new AuthorVm
                           {
                               AuthorId = author.AuthorId,
                               AuthorName = author.AuthorName,
                               BookCount = author.Books.Count()
                           };
                return authors;
            }
        }
    }


    • Now we have "Get method" in each controller ,  Run your project to test get request e.g. http://localhost:xxxx/api/books or http://localhost:xxxx/api/authors .


    • to configure retrieved formatter for the JSON data to retrieve Json "Camel case"  , In the App_Start folder open WebApiConfig.cs And add the following code to Register method :
    //WebApiConfig.cs

    var json = config.Formatters.JsonFormatter;

                json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

    link to project in github.