How to create Web API ?

Dear Friends,

In Last article https://indiandotnet.wordpress.com/2014/02/23/what-is-web-api/ 

We have discussed what is Web API. Now in this article we will see how to create web API. So with out wasting much time lets start visual studio (In my case I am running VS 2013 web express).

So in my example I am creating a web API project for Friends. this API should return either all the friends or specific friend according to Id so lets start.

Step 1: Create a  new web project when you click on web project you will get following option. Select Web API option as shown in below screen shot.

project-WebAPi

You will get default project as below with defined folder structure

defaultProject

The main point here is to remember Model class should come in model folder & Controller class should come in controller folder

Step 2:- Once you click on Web API option project will open. Now Create a model (a simple class) with name Friend. this model class have basic friend’s property like his firstName, LastName,Id etc. as shown in below fig

FriendModelClass

Step 3:- Now create  a Friend controller so for this just add new item which is controller as shown in below fig

AddWebAPIController

In the above option I have selected Web API 2 controller Empty template

things to remember here the name of controller class will be use later on in your API URL so select the name properly and the controller class name should followed by controller.

In my case I have created controller with following name =”FriendController.cs”. So now your project structure is as shown below

CurrentProject_Structure

Step 4:- Now remember all the coding part need to be done here . I am creating two method here one method with name “GetAllFriends” and “GetFriendById”.

So as name suggest one will return list of all friends and one will return particular friend for a given id. If you see below instead of fetching data from database I have created a array of friends as shown.

ControllerDefinition

Now in above fig you saw two method with different return type. Now concentrate on GetFriendByID It’s return type is HttpActionResult. so if you see below in that particular method the return type is OK which means status code 200. It means http response will be return in this method.

Now another interesting part is you can add different attribute with method like HTTPGET, HTTPPOST,HTTPDELETE etc Just see in below screen shot as shown below.

HttpGetOrOtherAttributes

Now another interesting point is what if you want two attribute on same method then you can use below option which shown “AcceptVerbs”

AcceptVerbs

Step 5:- Now our most part is done now you need to access web API but before accessing web API you need to check route and if something is missing their then you need to modify WebAPI config  which you will get in App_Start function. In my case I have added API/{controller}/{action}/{id} which was not exist earlier. as shown in below fig

WebAPIConfigSettings

Step 6:- Now we are good to go now just run the application and type in url http://localhost:yourport/API/Friend you will get list of all the friends as shown below

FindListOfAllFriends

Now if you want to get specific friend with particular id then you have to write url like http://localhost:youport/API/Friend/1

You will get particular friend with that id as shown below

FindingSpecificFriendByID

 

Isn’t it simple ?

I hope you understand how easy to create Web API. Now in next step we will learn how to consume the Web API.

Mean while you can create different web api.

So enjoy till than

Your host,

Rajat

Advertisements

What is Web API ?

HTTP is simple way to connect broad range of clients, including browser, mobile & desktop application. To achieve above broad range a new framework is introduce on top of .NET framework which is called ASP.NET web API.

With ASP.NET Web API you can call simple RESTFul service. You can perform various GET, PUT, POST, DELETE operation using Web API.

Web API is enable to service data request from client in both (JASON /XML) format according to client need.

Web API introduced with ASP.NET MVC framework in later stage. If you are familiar with MVC application then you will think it is same.

For example you want to get list of all the customers then a URL may be like below

http://mycustomerwebapi.com/Customers

Now suppose you want specific customer with customerId = 1 then you may need to write following URL in browser

http://mycustomerwebapi.com/customer/1

So above URL shows you can easily call the Web API services

You can easily host web API in IIS same as you host a web site.

I hope now we have a basic understanding of Web API.

In next topic we will discuss How to implement Web API.

Thanks & Best Regards,

Rajat Jaiswal

Get Admin rights to your local SQL server Express with this simple script–Unable to create database issue

 

Dear All,

Recently  I was facing the issue when login through my domain user account I am unable to create database. SQL Server throwing error like you don’t have permission.

By mistake I don’t have  “SA” username ‘s password.

I tried to search on internet  at after reading few articles I found below link which resolved my issue. It assign my domain user sysadmin permission.

I hope it will help you people also.

So enjoy downloading it and running it

http://archive.msdn.microsoft.com/addselftosqlsysadmin/ 

Thanks &  Best Regards,

Rajat Jaiswal

How to disable Right click on web page ?

Dear All,
Sometime, due to some security reference your application require to stop right click on web page.
In such case you need to write just simple code as shown below.

//
Right click disable

So now when you run your web page and right click you will get following message.

result
So just each code and you are good to go.

Hope you like it.

Thanks & Best Regards,
Rajat Jaiswal

Parse string in SQL Server

Dear friends,
Recently,
One of my colleagues was working on parsing on string in Sql server. I was thinking of old method like PATINDEX but he suggested a new way which is PARSENAME.

Below is basic syntax of it
PARSENAME (‘String with dot value’,index)
Example: suppose we have following string ‘234.456.678.454’
Now if we want to parse it and want 2nd item like 678 then you have to write following statement

DECLARE @StringToParse AS VARCHAR(200)
SET @StringToParse =’234.456.678.454’
SELECT PARSENAME (@StringToParse,2)

When you run above statement then you will get 678.
The mandatory condition here is the string will have only “.” Dot delimiter.
I hope you will also enjoy this new learning.

Thanks & Best Regards,
Rajat Jaiswal