Click here to Skip to main content
15,881,588 members
Articles / Web Development / HTML

Working With Test Client In ASP.NET Web API Help Page

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
31 May 2016CPOL4 min read 21.7K   99   1   4
Working With Test Client In ASP.NET Web API Help Page

In this article, we are going to see how we test our API with the help of a package called WebApiTestClient. As you all know, if you create a sample API project, you will get a folder HelpPage in Areas. This is for adding the XML description to each controller and actions we use in our API. If you document it well, any one can understand your API, so that you don’t need to explain what your API will do and what would be the output. If you are new to HelpPage implementation in API, please see here: API help page controller action description in Web API. Here, I am going to create a Web API with the help page descriptions in Visual Studio 2015. And once after the project is ready, I will install the WebApiTestClient package to my solution. I hope you will like this.

Please note that this package is not officially released by Microsoft. This is a prototype created by Yao – MSFT

Background

For the past few months, I had been working with API projects. Recently, I was asked to create an application to test the API I created so that the testing team can test the API easily. Yes, I agree that we have tools like Fiddler and Post Man for the same. Still, we thought to create our own. As I started my development, I came to know about the package WebApiTestClient which does what we wanted. Finally, we decided to stop developing the application and used this wonderful package. Here, I am going to show you how can we use this.

Prerequisites

  • Visual Studio With Web API Installed

Things We Are Going To Do

The following are the tasks we are going to do.

  • Setting up database
  • Creating an Entity Framework
  • Creating API controller with the Model Created
  • Installing WebApiTestClient
  • Configuring WebApiTestClient
  • Testing WebApiTestClient

Setting Up Database

Here, I am going to create a database which I created for my demo purposes. You can always create this database by running the queries mentioned here.

Create Database

SQL
USE [master]
GO

/****** Object:  Database [TrialsDB]    Script Date: 5/12/2016 10:56:41 AM ******/
CREATE DATABASE [TrialsDB]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'TrialsDB', _
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TrialsDB.mdf' , _
SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'TrialsDB_log', _
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TrialsDB_log.ldf' , _
SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

ALTER DATABASE [TrialsDB] SET COMPATIBILITY_LEVEL = 110
GO

IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [TrialsDB].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO

ALTER DATABASE [TrialsDB] SET ANSI_NULL_DEFAULT OFF 
GO

ALTER DATABASE [TrialsDB] SET ANSI_NULLS OFF 
GO

ALTER DATABASE [TrialsDB] SET ANSI_PADDING OFF 
GO

ALTER DATABASE [TrialsDB] SET ANSI_WARNINGS OFF 
GO

ALTER DATABASE [TrialsDB] SET ARITHABORT OFF 
GO

ALTER DATABASE [TrialsDB] SET AUTO_CLOSE OFF 
GO

ALTER DATABASE [TrialsDB] SET AUTO_CREATE_STATISTICS ON 
GO

ALTER DATABASE [TrialsDB] SET AUTO_SHRINK OFF 
GO

ALTER DATABASE [TrialsDB] SET AUTO_UPDATE_STATISTICS ON 
GO

ALTER DATABASE [TrialsDB] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO

ALTER DATABASE [TrialsDB] SET CURSOR_DEFAULT  GLOBAL 
GO

ALTER DATABASE [TrialsDB] SET CONCAT_NULL_YIELDS_NULL OFF 
GO

ALTER DATABASE [TrialsDB] SET NUMERIC_ROUNDABORT OFF 
GO

ALTER DATABASE [TrialsDB] SET QUOTED_IDENTIFIER OFF 
GO

ALTER DATABASE [TrialsDB] SET RECURSIVE_TRIGGERS OFF 
GO

ALTER DATABASE [TrialsDB] SET  DISABLE_BROKER 
GO

ALTER DATABASE [TrialsDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO

ALTER DATABASE [TrialsDB] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO

ALTER DATABASE [TrialsDB] SET TRUSTWORTHY OFF 
GO

ALTER DATABASE [TrialsDB] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO

ALTER DATABASE [TrialsDB] SET PARAMETERIZATION SIMPLE 
GO

ALTER DATABASE [TrialsDB] SET READ_COMMITTED_SNAPSHOT OFF 
GO

ALTER DATABASE [TrialsDB] SET HONOR_BROKER_PRIORITY OFF 
GO

ALTER DATABASE [TrialsDB] SET RECOVERY FULL 
GO

ALTER DATABASE [TrialsDB] SET  MULTI_USER 
GO

ALTER DATABASE [TrialsDB] SET PAGE_VERIFY CHECKSUM  
GO

ALTER DATABASE [TrialsDB] SET DB_CHAINING OFF 
GO

ALTER DATABASE [TrialsDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO

ALTER DATABASE [TrialsDB] SET TARGET_RECOVERY_TIME = 0 SECONDS 
GO

ALTER DATABASE [TrialsDB] SET  READ_WRITE 
GO

Create Table with Data

SQL
USE [TrialsDB]
GO
/****** Object:  Table [dbo].[Product]    Script Date: 5/12/2016 10:54:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Product](
	[ProductID] [int] NOT NULL,
	[Name] [nvarchar](max) NOT NULL,
	[ProductNumber] [nvarchar](25) NOT NULL,
	[MakeFlag] [bit] NOT NULL,
	[FinishedGoodsFlag] [bit] NOT NULL,
	[Color] [nvarchar](15) NULL,
	[SafetyStockLevel] [smallint] NOT NULL,
	[ReorderPoint] [smallint] NOT NULL,
	[StandardCost] [money] NOT NULL,
	[ListPrice] [money] NOT NULL,
	[Size] [nvarchar](5) NULL,
	[SizeUnitMeasureCode] [nchar](3) NULL,
	[WeightUnitMeasureCode] [nchar](3) NULL,
	[Weight] [decimal](8, 2) NULL,
	[DaysToManufacture] [int] NOT NULL,
	[ProductLine] [nchar](2) NULL,
	[Class] [nchar](2) NULL,
	[Style] [nchar](2) NULL,
	[ProductSubcategoryID] [int] NULL,
	[ProductModelID] [int] NULL,
	[SellStartDate] [datetime] NOT NULL,
	[SellEndDate] [datetime] NULL,
	[DiscontinuedDate] [datetime] NULL,
	[rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
	[ModifiedDate] [datetime] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

INSERT [dbo].[Product] ([ProductID], [Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], _
[Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], _
[SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], _
[Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], _
[DiscontinuedDate], [rowguid], [ModifiedDate]) VALUES (1, N'Adjustable Race', N'AR-5381', _
0, 0, NULL, 1000, 750, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, _
NULL, CAST(0x0000921E00000000 AS DateTime), NULL, NULL, N'694215b7-08f7-4c0d-acb1-d734ba44c0c8', _
CAST(0x00009A5C00A53CF8 AS DateTime))

INSERT [dbo].[Product] ([ProductID], [Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], _
[Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], _
[SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], _
[Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], _
[DiscontinuedDate], [rowguid], [ModifiedDate]) VALUES (2, N'Bearing Ball', N'BA-8327', 0, 0, _
NULL, 1000, 750, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, _
CAST(0x0000921E00000000 AS DateTime), NULL, NULL, N'58ae3c20-4f3a-4749-a7d4-d568806cc537', _
CAST(0x00009A5C00A53CF8 AS DateTime))

INSERT [dbo].[Product] ([ProductID], [Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], _
[Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], _
[SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], _
[Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], _
[DiscontinuedDate], [rowguid], [ModifiedDate]) VALUES (3, N'BB Ball Bearing', N'BE-2349', _
1, 0, NULL, 800, 600, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, _
CAST(0x0000921E00000000 AS DateTime), NULL, NULL, N'9c21aed2-5bfa-4f18-bcb8-f11638dc2e4e', _
CAST(0x00009A5C00A53CF8 AS DateTime))

INSERT [dbo].[Product] ([ProductID], [Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], _
[Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], _
[SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], _
[Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], _
[DiscontinuedDate], [rowguid], [ModifiedDate]) VALUES (4, N'Headset Ball Bearings', _
N'BE-2908', 0, 0, NULL, 800, 600, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 0, NULL, NULL, _
NULL, NULL, NULL, CAST(0x0000921E00000000 AS DateTime), NULL, NULL, _
N'ecfed6cb-51ff-49b5-b06c-7d8ac834db8b', CAST(0x00009A5C00A53CF8 AS DateTime))

INSERT [dbo].[Product] ([ProductID], [Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], _
[Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], _
[SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], _
[Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], _
[DiscontinuedDate], [rowguid], [ModifiedDate]) VALUES (316, N'Blade', N'BL-2036', 1, 0, _
NULL, 800, 600, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, _
CAST(0x0000921E00000000 AS DateTime), NULL, NULL, N'e73e9750-603b-4131-89f5-3dd15ed5ff80', _
CAST(0x00009A5C00A53CF8 AS DateTime))

INSERT [dbo].[Product] ([ProductID], [Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], _
[Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], _
[SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], _
[Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], _
[DiscontinuedDate], [rowguid], [ModifiedDate]) VALUES (317, N'LL Crankarm', N'CA-5965', 0, 0, _
N'Black', 500, 375, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 0, NULL, N'L ', NULL, NULL, NULL, _
CAST(0x0000921E00000000 AS DateTime), NULL, NULL, N'3c9d10b7-a6b2-4774-9963-c19dcee72fea', _
CAST(0x00009A5C00A53CF8 AS DateTime))

INSERT [dbo].[Product] ([ProductID], [Name], [ProductNumber], [MakeFlag], [FinishedGoodsFlag], _
[Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], _
[SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], _
[Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], _
[DiscontinuedDate], [rowguid], [ModifiedDate]) VALUES (318, N'ML Crankarm', N'CA-6738', 0, 0, _
N'Black', 500, 375, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 0, NULL, N'M ', NULL, NULL, NULL, _
CAST(0x0000921E00000000 AS DateTime), NULL, NULL, N'eabb9a92-fa07-4eab-8955-f0517b4a4ca7', _
CAST(0x00009A5C00A53CF8 AS DateTime))

Our database is ready, now create a Web API application in Visual Studio and then entity with the above mentioned database.

Creating Entity

Creating Entity

If you don’t know how to create an entity in your solution, please read that here. I have mentioned the steps to be followed in that article. Once you have created the entity, you are good to go and create an API controller with the entity created. If you do so, the CRUD actions will be created automatically for you. You may need to edit those actions according to your needs.

Web API 2 Controller with actions, using Entity Framework

Web API 2 Controller with actions, using Entity Framework

Select the Model Class, DBContext then name your controller and click OK. I hope a controller with the CRUD actions. Now we can modify that as follows:

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using ControllerActionDescriptions.Models;

namespace ControllerActionDescriptions.Controllers
{
    public class ProductsController : ApiController
    {
        private TrialsDBEntities db = new TrialsDBEntities();
        #region GetProducts
        /// <summary>
        /// Get all the products available
        /// GET: api/Products
        /// </summary> 
        public IQueryable<Product> GetProducts()
        {
            return db.Products;
        }
        #endregion

        #region GetProductWithParameter
        /// <summary>
        /// Get a single product by id
        /// GET: api/Products/5
        /// <param name="id"></param>
        /// </summary> 
        [ResponseType(typeof(Product))]
        public IHttpActionResult GetProduct(int id)
        {
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return NotFound();
            }

            return Ok(product);
        }
        #endregion
 // PUT: api/Products/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != product.ProductID)
            {
                return BadRequest();
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Products
        [ResponseType(typeof(Product))]
        public IHttpActionResult PostProduct(Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Products.Add(product);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (ProductExists(product.ProductID))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = product.ProductID }, product);
        }

        // DELETE: api/Products/5
        [ResponseType(typeof(Product))]
        public IHttpActionResult DeleteProduct(int id)
        {
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return NotFound();
            }

            db.Products.Remove(product);
            db.SaveChanges();

            return Ok(product);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ProductExists(int id)
        {
            return db.Products.Count(e => e.ProductID == id) > 0;
        }
    }
}

Installing WebApiTestClient

To install the package, please go to your Package Manage Console from NuGet Package Manager and run the following command.

C#
Install-Package WebApiTestClient

You can always get the details about the package here.

Once you install the package, you can see some files are added to your Script and Area folder as preceding.

Script and Area Folder

Script and Area Folder

Configuring WebApiTestClient

To configure the WebApiTestClient, please go to the folder Areas->Views->Help and then click on Api.cshtml.

Api Cshtml

Api Cshtml

This is the view shown when you click on each API in your help page. Now add the preceding code block to that view.

HTML
@Html.DisplayForModel("TestClientDialogs")
@section Scripts
{
<link href="~/Areas/HelpPage/HelpPage.css" rel="stylesheet" type="text/css" />
    @Html.DisplayForModel("TestClientReferences")
}

Code Block

Code Block

Testing WebApiTestClient

Now run your API application and go to the help page for any controller action, you can see a button called Test API on the bottom. If you click on that, you will get a pop where you can test your API action.

Test API Client Output

Test API Client Output

Now if you send your request by clicking the send button, you will get an output as follows:

Test API Client Output With Response

Test API Client Output With Response

You can always give id parameter as follows:

Test Client With Parameters

Test Client With Parameters

You can also give content-length and content-type in your post request as follows:

Test Client With Post

Test Client With Post

Test Client With PUT Request

Test Client With PUT Request

References

Author has already posted the source code in GitHub, please check here.

Have a happy coding!

Conclusion

Did I miss anything that you may think is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Your Turn. What Do You Think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, ASP.NET Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
I am Sibeesh Venu, an engineer by profession and writer by passion. I’m neither an expert nor a guru. I have been awarded Microsoft MVP 3 times, C# Corner MVP 5 times, DZone MVB. I always love to learn new technologies, and I strongly believe that the one who stops learning is old.

My Blog: Sibeesh Passion
My Website: Sibeesh Venu

Comments and Discussions

 
QuestionIs this NuGet discontinued? Pin
Łukasz Lewandowski20-Aug-18 10:40
Łukasz Lewandowski20-Aug-18 10:40 
GeneralMy vote of 5 Pin
Santhakumar M1-Jun-16 23:09
professionalSanthakumar M1-Jun-16 23:09 
GeneralRe: My vote of 5 Pin
Sibeesh Passion1-Jun-16 23:17
professionalSibeesh Passion1-Jun-16 23:17 
GeneralRe: My vote of 5 Pin
Santhakumar M1-Jun-16 23:45
professionalSanthakumar M1-Jun-16 23:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.