Please enable Javascript for better experience...
Web API Tutorial C# - Part 2: Creating MVC with Web API Application
By Rahul Kumar Jha | Jan 3, 2020 | In Tutorials | Total Views [ 5703 ]
Taged In
(4 Like)
Rate

This is part 2 of Web API Tutorial series. In this article you will learn creating a MVC + Web API application. We will create a basic MVC application, a Web API and learn to consume Web API using HTTPClient.

Introduction

This tutorial is divided into three part - Creating MVC application, Creating Web API and Consuming Web API into MVC. We will do it one by one so that you can follow all the steps. You can also go through this video, whcih has all the steps explain in detail. I am also planning to push code in GitHub and may be I record a video for GitHub too.

Create MVC Application

Step 1) Open Visual Studio and Create a web project.

01

Step 2) Name your project and select MVC template.

02

Step 3) A default Scaffold template generates for MVC having HomeController.

03

Create Web API Application

Step 4) Same steps apply to Web API application as well, just select Empty project. You can select Web API template as well but it includes both MVC and Web API templates. In this tutorial we are considering both application as separate entities so we created separate projects. Though you can create separate solution as well but this also convey same message.

04

Step 5) Apart from these two projects, create a class library as well. We will use this as ModelLayer.

05

Step 6) Add reference of ModelLayer into Web API and MVC projects. Model layer is common for both applications.

Adding Code

Step 7) Once we are done with project creation and referencing part, now it's time to add some code stuff. We will start with ModelLayer. Add "Employee" class and below code.

06

Step 8) Now Add a Web API controller into Web API application. For this, right click on Controller folder in Web API project and click "Add New Item" and add controller. Name this controller as "EmployeeController".

07

Step 9) A default code with default methods for HTTP verbs GET, POST, PUT, UPDATE and DELETE are generated. Delete all code and add below code. Add reference of model layer into controller.

08

Step 10) Build the application and start debugger. Enter URL "api/employee" in browser and you should see below result.

09

Step 11) Open project properties and change the default port. You can skip this step if you want.

10

Step 12 ) Now open MVC application and add below code in HomeController.

11

12

13

Step 13) Now add below code in Index.cshtml

14

15

Step 14) Set multi startup projects. Set MVC and Web API projects as startup projects.

16

If you want to learn more about setting multiple startup projects, please watch this video. You can follow this link as well Running multiple projects.

Step 15) Run the application and check the output.

17

In next tutorial we will learn adding authentication in Web API. Till then, please stay connected.

<< WEB API Tutorial C# - An Introduction to Web API
Web API Tutorial C# - Part 3: Implementing basic Bearer authentication in Web API application >>

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors