ASP.NET Core Web API Tutorial – Part 2

By | September 25, 2017
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest

This is part-2 in series of ASP.NET Core Web API Tutorials. In previous part, we discussed in details about ASP.NET Core Web API Architecture and new features. Along with that we implement a comprehensive step by step example using Fiddler to consume a publicly available RESTful Service.

In Part-2, we will continue this practical approach and implement a basic ASP.NET Core Web API with proper CRUD (Create, Retrieve, Update, Delete) operations.

Your First ASP.NET Core Web API

Before diving further to create an ASP.NET Core Web API, let’s first understand few related concepts.

From the following figure, View act as the user interface and user send request to controller through View. Controller maps URL to associated action method for each different request from the view and perform operation on model. On successful operation and changing the state of the model controller renders the current state of the model to the view as response. And finally, view display the current state of the model.Model View Controller

Controller:

  • MVC framework maps URLs to classes, and this class that communicates between view and model are called controllers
  • Controller defines the application behavior by processing incoming requests from view, handling user input and interactions from user interface/ view, and execute appropriate application logic in order to manipulate the state of the model.
  • Each controller class is mapped to a separate view component and generate the HTML markup for the request from that associated view.
  • In ASP .NET, the base class for all controllers is the ControllerBase class, which provides general MVC handling.
  • Controller class inherited from ControllerBase class locates the appropriate action method to call and validating that it can be called for each individual request from view.
  • Handles input parameters to use as the action method’s arguments.
  • Handles all possible errors and exception that might occur during the execution of the action method.
  • Associated with the default WebFormViewEngine class in order to render ASP.NET page types (views).

Action Methods:

  • For each request from view there is an associated action method in corresponding controller
  • Action methods typically a one-to-one mapping with user interactions in view, for example, if a user wants to login there is a associated action method in login controller, for forget password there is another action method in the same controller.

Model:

  • Encapsulate the state of the application
  • For example in case of a book library application, book is one of the model that represents the book collection for the application.
  • It is an object that represents the data of the application.
  • NET Web API can automatically serialize model to JSON, XML, or some other format
  • It is possible to write the serialized data into the body of the HTTP response message. And later, it can de-serialize the object.

Career skills to jumpstart your future.

Basic ASP.NET Core Web API Implementation:

Following are the steps for creating a MVC project using ASP .NET:

  • Create a web project
  • Start Visual Studio and select New Project from the Start page. Or, from the File menu, select New and then Project.
  • In the Templates pane, select Installed Templates and expand the Visual C# node. Under Visual C#, select Web. In the list of project templates, select ASP.NET Web Application. Name the project “BooksApp” and click OK.ASP.NET Core Web API Project
  • In the New ASP.NET Project dialog, select the MVC. Under “Add folders and core references for”, check Web API. Click OK.
  • If Solution Explorer is not already visible, click the View menu and select Solution Explorer. In Solution Explorer, right-click the Models folder. From the context menu, select Add then select Class.Visual Studio 2017
    Book Class
  • This will create the Book.cs Model as following:Book Model Web API
  • Add necessary field to the Book Model as following:Model in ASP.NET Core Web API
  • In Web API, a controller is an object that handles HTTP requests. We’ll add a controller that can would have necessary action method to perform CRUD operation on a model . Right click the controllers folder, select Add. Select Web Api Controller class (v 2.1).ASP.NET Core Web API Controller
  • The following class would be generated:BooksController as Web API Controller
  • Follow the steps on the following windows, here we have to perform several action
    • Right click the Solution on the right side solution explorer window and select “Manage NuGet Packages for Solution”.
    • Check the” include prerelease”.
    • Click Browse and on the search text box , type “mongo”.
    • From the Search result select “MonGODB.Driver”.Manage NuGet Package for Solution
  • This will open a new window  with details from MongoDB Driver as following:MongoDB Driver
    From the above window, select the project and for the version select 2.3.0-rc1. Click “Install”
  • Accept the License agreement.MongoDB License Agreement
  • In the Add Controller dialog, name the controller “BooksController”. Click Add.
  • Make sure the installation is successful.NuGet Package Manager
  • Now update the Books.controller class as following:
  • Click Build Solution from ‘Build’ menu and click Google Chrome from the menu . The application will start and on the chrome browser.ASP.NET Core Web API Application

We will be using the above implementation further in future ASP.NET Core Web API Tutorials. So, keep in touch.

ASP.NET Core with Angular 2
Build a Real-world App with ASP.NET Core and Angular 2 (4+)

  • Build a full-stack web app with ASP.NET Core, Entity Framework Core and Angular 2 & 4.
  • Implement a Clean & Decouple Architecture.
  • Properly implement the Repository and Unit of work patterns.
  • Troubleshoot common runtime errors.
  • Test APIs using PostMan.
  • Integrate ASP.NET MVC/Core with AngularJS.
  • Understand and apply the Dependency Inversion Principle (DIP).
  • Use the new dependency injection feature in ASP.NET Core.
  • Build APIs with ASP.NET Core
  • and more….
Step forward in 2017: Build in-demand career skills with Coursera

More Web Development and related Tutorials
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest