You will learn two major things in this article:
- Create GET Custom Method Web API using .Net Framework.
- Testing Web API GET Method using Talend Chrome Browser Extension.
We are going acheive the above two major things with the help of the followings steps,
- Create SQL Table and Insert Data using Query
- Asp.Net Web API Application Creation
- Linq to Sql Class
- Write GET Custom Method of Web API
- Test Asp.Net Web API GET Method in Browser
- Install Talend Extension in Chrome Browser
- Test Asp.Net Web API GET Method for XML and JSON output.
Create SQL Table and Insert Data using Query
Connect to your SQL Server,
Table Creation SQL Script,
- USE [MbkTest]
- go
- /****** Object: Table [dbo].[tblFriends] Script Date: 07-Jan-20 12:53:31 PM ******/
- SET ansi_nulls ON
- go
- SET quoted_identifier ON
- go
- SET ansi_padding ON
- go
- CREATE TABLE [dbo].[tblfriends]
- (
- [friendid] [INT] IDENTITY(1, 1) NOT NULL,
- [friendname] [VARCHAR](100) NULL,
- [phonenumber] [VARCHAR](50) NULL,
- [emailid] [VARCHAR](100) NULL,
- [country] [VARCHAR](100) NULL,
- [dob] [DATETIME] NULL,
- [ismarried] [BIT] NULL,
- [salaryperannum] [NUMERIC](10) NULL,
- PRIMARY KEY CLUSTERED ( [friendid] ASC )WITH (pad_index = OFF,
- statistics_norecompute = OFF, ignore_dup_key = OFF, allow_row_locks = on,
- allow_page_locks = on) ON [PRIMARY]
- )
- ON [PRIMARY]
- go
- SET ansi_padding OFF
- go
Insert Table Data Using SQL Script,
- insert into tblFriends values ('Suresh Vyas','98612134512','[email protected]','India',1990/01/01,1,450000)
- insert into tblFriends values ('Manoj Kalla','9869166077','[email protected]','India',1980/02/05,1,950000)
- insert into tblFriends values ('Kamlesh Goenka','981916607','[email protected]','India',1970/02/06,1,750000)
- insert into tblFriends values ('Prashant Bansal','781214607','[email protected]','US',1989/01/04,01,250000)
- insert into tblFriends values ('Nitin Tiwari','811012607','[email protected]','US',1992/01/04,0,350000)
- insert into tblFriends values ('Mahesh Jadhav','921052607','[email protected]','US',1998/01/06,0,42000)
Select Query to Check the Records,
- select * from [dbo].[tblFriends]
Create ASP.NET Web Application project called “LearnWebApi”
You can check in the above screen shot that the MVC and Web API checkboxes are selected by default.
Project is created successfully.
Connecting to MBKTEST database
For Server Explorer click on VIEW --->Server Explorer
Now insert LINQ TO SQL,
Right click on Project Select ADD--> NEW ITEM
Select Data then select “LINQ TO SQL CLASSES”.
Drag tblFriends table on (MbkTestDataClasses.dbml) Linq To SQL class.
Switch to default controller that is ValuesController (ValueController.cs file) which is located inside root’s Controller folder.
Code
- [HttpGet]
- [Route("api/FriendListMsg")]
- public HttpResponseMessage GetFriendList()
- {
- MbkTestDataClassesDataContext db = new MbkTestDataClassesDataContext();
- var FriendList = (from a in db.tblFriends select a).ToList();
- return Request.CreateResponse(HttpStatusCode.OK, FriendList);
- }
Press F5 and Check on browser.
Just type ADDRESS
http://localhost:50297/api/FriendListMsg
How to install Talend API Tester?
Open Chorme browser, click on the top left side and there is a button called APPS
Or
Type on Address Bar:chrome://apps/
Select WEB STORE
Search Text Box type ---> “Talend API Tester”
After extension installation
Now run the Talend API Tester by clicking on the right top side.
Sending application/xml request to server to get output in XML format.
XML OUTPUT RECEIVED FROM SERVER,
Sending application/json request to server to get output in JSON format.
JSON OUTPUT RECEIVED FROM SERVER,