Create Web API Application using .Net Core MVC

Most of the people feel difficulty in creating web api application .  You might feel difficulty in creating it.

Web Api  application with .Net core

I will make easy for you to create web api application . Before that i need to give you an Overview  of web api & .Net Core

 

What is WebAPIs ?  What are web apis used for ?

 

  • API stands for Application Program Interface. Webapis MEANS API which runs on Internet .
  • Mircosoft webapi sits between two application & it allows application to interact.

 

Real Life Example:

When your wife & your mom has problem with each other. They don’t interact directly.  Whatever your mom says about your wife you listen it and tell it to your wife . Your wife perform some action & says something for your mom then you go to your mom  tell her.

 

In  this example :

Your Wife & Mom are two application.

You are the web api who is transferring data between them. Or a medium that help them to interact

 

I think it is very much clear about WEB API. Lets talk about .Net Core.

 

.Net Core

  • It is .Net Framework , Known my name Asp.net Core 1.x ,Asp.net 2.x
  • It is free & open source Software Framework
  • It works on well known different operating System like Windows,macOS & Linux

For more Detail of .Net Core follow below links.

  1. Prerequisites for .NET Core -.NET Core wiki
  2. Getting started with .NET Core 2 Console application
  3. Powerful way to Create Web App in Asp.net Core MVC

 

As we know the basics lets get start making web api application.

 

Get  started Web Api  application with .Net core

Open Visual Studio 2017 File->New Project , Then Select .Net Core & Asp.net Core Web Application ,Name your project webapi press OK.

 

Now you will select empty  and click ok.

Run your project Ctrl+F5 and you will see Hello World!.

This hello world message comes from the Startup.cs

app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
 });

 

Now we will create a folder in our project and Rename as Controllers

And Right click on Controllers folder , Go to Add & New Item .

c# apicontroller

Now Select Asp.net Core & Add Web Api Controller Class name it as ValuesController.cs

ValuesController class will have different http services for Get ,Post ,Put ,Delete

[HttpGet], [HttpPost], [HttpPut(“{id}”)], [HttpDelete(“{id}”)]

 

Open Startup.cs this page is used for adding services and routing the application

First of all we will add mvc services

public void ConfigureServices(IServiceCollection services)
 {
  services.AddMvc();
}

And add routing of application

Replace this code

app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});

With this

app.UseMvc(routes =>
{
 routes.MapRoute(
 name: "default",
 template: "{controller=api}/{action=Values}/{id?}"); 

routes.MapSpaFallbackRoute(
 name: "spa-fallback",
 defaults: new { controller = "api", action = "Values" });
     });

This will give route to our controller class ValuesController.cs

Now we will you different working of API

Get

Run your project Ctrl+F5 & append /api/Values in  http://localhost:xxxxx/ your uri will look like http://localhost:xxxxx/api/values. This Uri fetches the get method

// GET: api/controller
[HttpGet]
public IEnumerable<string>Get()
 {
return new string[] { "value1", "value2" };
 }

Lets see what happen if we pass some id to the uri http://localhost:63648/api/values/5

Now our api fetches the data from controller & print values

// GET api/controller/5
[HttpGet("{id}")]
public string Get(int id)
{
 return "value";
 }

Post

For posting data to api we need some other application but here we don’t have any application so i will use one freeware software POSTMAN . Download this software from internet.

We will run our project on Visual studio .Then Copy Uri http://localhost:63648/api/values. Paste in Postman & select POST in that.

 

As we know we need to pass body to the POSTMAN see below video for POST in postman.

PUT

For PUT we will pass some id to uri : Follow video below

DELETE

For Delete we will pass some id to uri : Follow video below

This is the whole process for WEB API . Hope you understand the functioning of WEBAPI using .NET CORE

If you find any difficulty please ask.

  •  
    56
    Shares
  • 56
  •  
  •  
  •  
  •  
  •  

About the Author

Prashant Goswami

I am the blog writer who will teach how to love programming. My experience in programming is about 6 years. And i am the lover of programming , so i will make you people.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: