Create Simple Web API 2 Project in few minutes - .NET Framework - Tutorial NO. 01
In the first tutorial in this series, I will show you how to create simple Web API project
You can download the sample project file in here. Download "SimpleWebAPI"sample project
You can download the sample project file in here. Download "SimpleWebAPI"sample project
- Open visual studio in your PC. For this tutorial i will use Visual Studio 2017.
- Go to File > New > Project
- And Select Installed > Visual C# > Web > ASP.NET Web Application (.NET Framework)
- Name your project as "SimpleWebAPI". Then choose file location to save your solution and click OK.
- Next step is select template for your project.
- For this tutorial i will create project without using any built in content like MVC or Web Forms project. Select Empty project template and select Web API for core reference. (See image below)
- Then Click OK.
- Than Visual Studio will create solution with your project.
- It include files in following image
- For first tutorial, I will create simple Web API 2 controller
- For than Right Click on "Controllers" folder in solution explore > Add > Controller (See following Image)
- Select Web API 2 Controller - Empty and Click Add.
- Then VS popup to insert controller name. Type "HomeController" and Click Add.
- It will create Empty class file with "using System.Web.Http" namespace. You can see our "HomeController" class is inheriting from "ApiController" abstract class.
- It have all the properties and methods and we can define those in our class to create Web API.
- Now we are going to create simple GET request in our project
- Now add following code to "HomeController" Class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace SimpleWebAPI.Controllers
{
public class HomeController : ApiController
{
[HttpGet]
public IHttpActionResult Get()
{
return Ok("Hi, I am Ishara");
}
}
}
- In above code i created simple HTTP GET request. There are other methods like POST, PUT, DELETE i will describe in future tutorials.
- Remember i put "[HttpGet]" annotation top of the Get method. You don't need to put that on the method if the name of the action method include "Get" in the beginning. (Ex : GetID, GetName, GetProduct etc.) . Otherwise you have to put [HttpGet] on the action method.
- Then Right-Click on your solution and click "Build Solution"
- Debug project by pressing F5 key. In my configuration by default it opens with Google Chrome. You can select any browser that you want.
- After opening the browser replace browser URL with "http://{your localhost}/apu/Home" (In my case 'http://localhost:60823/api/Home')
- Then you can see result like following image
- By default Web API 2 return XML result. That's the reason our output in contain XML codes.
- In My next tutorial i will explain how to get JSON (Java Script object Notation) response from our Web API. Because now a days JSON is much popular than XML and we are going to continue our tutorial with JSON
This is enough for the first tutorial please ask any problems regarding this post, code in the comment section. see you on next tutorial.
Really nice blog post. provided a helpful information. I hope that you will post
ReplyDeletemore updates like this
Dot NET Online Course Bangalore
Awesome blog and an innovative....
ReplyDeletejava training in navalur
selenium training in navalur
ReplyDeleteSuch an interesting blog for this technology really helpful for me.
waiting for new post. Keep updating.
dot net training in chennai
dot net course in chennai
ielts coaching in chennai
machine learning course in chennai
oracle training in chennai
dot net training in t.nagar
dot net training in omr
Great articles, first of all Thanks for writing such lovely Post! Other wise If any one who want to learn dot-net contact us on 9311002620 or visit:- https://htsindia.com/Courses/microsoft-courses/dotnettraininginstituteinsouthdelhi
ReplyDeleteperde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
dedektör
web site kurmak
Ask kitaplari
Smm Panel
ReplyDeletesmm panel
Https://isilanlariblog.com
İnstagram takipçi satın al
hirdavatciburada.com
Beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi