<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WebApiTestClient &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/webapitestclient/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 02 Jun 2021 15:23:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>WebApiTestClient &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Working With Test Client In Asp Net Web API Help Page</title>
		<link>https://www.sibeeshpassion.com/working-with-test-client-in-asp-net-web-api-help-page/</link>
					<comments>https://www.sibeeshpassion.com/working-with-test-client-in-asp-net-web-api-help-page/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Tue, 31 May 2016 00:00:33 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Web API]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[How to test Web API]]></category>
		<category><![CDATA[Test Web API]]></category>
		<category><![CDATA[Web API Tester]]></category>
		<category><![CDATA[WebApiTestClient]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11654</guid>

					<description><![CDATA[[toc] Introduction 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&#8217;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: [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>In this article we are going to see how we test our API with the help of a package called <em>WebApiTestClient</em>. 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&#8217;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: <a href="http://sibeeshpassion.com/working-with-api-help-page-controller-action-description-in-web-api/" target="_blank" rel="noopener">API help page controller action description in Web API</a>. Here I am going to create a <a href="http://sibeeshpassion.com/category/web-api" target="_blank" rel="noopener">Web API</a> with the help page descriptions in <a href="http://sibeeshpassion.com/category/tools/visual-studio/" target="_blank" rel="noopener">Visual Studio</a> 2015. And once after the project is ready I will install the <em>WebApiTestClient </em> package to my solution. I hope you will like this.</p>
<blockquote><p>Please be noted that this package is not officially released by Microsoft. This is a prototype created by <a href="https://social.msdn.microsoft.com/profile/Yao+-+MSFT" target="_blank" rel="noopener">Yao &#8211; MSFT</a></p></blockquote>
<h2><strong>Download the source code</strong></h2>
<p>You can always download the source code here: <a href="https://code.msdn.microsoft.com/Working-With-Test-Client-9fb65ba9" target="_blank" rel="noopener">API Test Client</a></p>
<h2><strong>Background</strong></h2>
<p>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 <em>WebApiTestClient</em> 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.</p>
<h2><strong>Prerequisites</strong></h2>
<ul>
<li>Visual Studio With Web API Installed</li>
</ul>
<h2><strong>Things we are going to do</strong></h2>
<p>The following are the tasks we are going to do.</p>
<ul>
<li>Setting up database</li>
<li>Creating an Entity Framework</li>
<li>Creating API controller with the Model Created</li>
<li>Installing <em>WebApiTestClient</em></li>
<li>Configuring <em>WebApiTestClient</em></li>
<li>Testing <em>WebApiTestClient</em></li>
</ul>
<h2><strong>Setting up database</strong></h2>
<p>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.</p>
<h3><strong>Create database</strong></h3>
<p>[sql]<br />
USE [master]<br />
GO</p>
<p>/****** Object: Database [TrialsDB] Script Date: 5/12/2016 10:56:41 AM ******/<br />
CREATE DATABASE [TrialsDB]<br />
CONTAINMENT = NONE<br />
ON PRIMARY<br />
( NAME = N&#8217;TrialsDB&#8217;, FILENAME = N&#8217;C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TrialsDB.mdf&#8217; , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )<br />
LOG ON<br />
( NAME = N&#8217;TrialsDB_log&#8217;, FILENAME = N&#8217;C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TrialsDB_log.ldf&#8217; , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET COMPATIBILITY_LEVEL = 110<br />
GO</p>
<p>IF (1 = FULLTEXTSERVICEPROPERTY(&#8216;IsFullTextInstalled&#8217;))<br />
begin<br />
EXEC [TrialsDB].[dbo].[sp_fulltext_database] @action = &#8216;enable&#8217;<br />
end<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET ANSI_NULL_DEFAULT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET ANSI_NULLS OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET ANSI_PADDING OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET ANSI_WARNINGS OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET ARITHABORT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET AUTO_CLOSE OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET AUTO_CREATE_STATISTICS ON<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET AUTO_SHRINK OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET AUTO_UPDATE_STATISTICS ON<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET CURSOR_CLOSE_ON_COMMIT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET CURSOR_DEFAULT GLOBAL<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET CONCAT_NULL_YIELDS_NULL OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET NUMERIC_ROUNDABORT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET QUOTED_IDENTIFIER OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET RECURSIVE_TRIGGERS OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET DISABLE_BROKER<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET DATE_CORRELATION_OPTIMIZATION OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET TRUSTWORTHY OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET ALLOW_SNAPSHOT_ISOLATION OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET PARAMETERIZATION SIMPLE<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET READ_COMMITTED_SNAPSHOT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET HONOR_BROKER_PRIORITY OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET RECOVERY FULL<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET MULTI_USER<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET PAGE_VERIFY CHECKSUM<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET DB_CHAINING OFF<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET TARGET_RECOVERY_TIME = 0 SECONDS<br />
GO</p>
<p>ALTER DATABASE [TrialsDB] SET READ_WRITE<br />
GO<br />
[/sql]</p>
<h3><strong>Create table with data</strong></h3>
<p>[sql]<br />
USE [TrialsDB]<br />
GO<br />
/****** Object: Table [dbo].[Product] Script Date: 5/12/2016 10:54:48 AM ******/<br />
SET ANSI_NULLS ON<br />
GO<br />
SET QUOTED_IDENTIFIER ON<br />
GO<br />
CREATE TABLE [dbo].[Product](<br />
[ProductID] [int] NOT NULL,<br />
[Name] [nvarchar](max) NOT NULL,<br />
[ProductNumber] [nvarchar](25) NOT NULL,<br />
[MakeFlag] [bit] NOT NULL,<br />
[FinishedGoodsFlag] [bit] NOT NULL,<br />
[Color] [nvarchar](15) NULL,<br />
[SafetyStockLevel] [smallint] NOT NULL,<br />
[ReorderPoint] [smallint] NOT NULL,<br />
[StandardCost] [money] NOT NULL,<br />
[ListPrice] [money] NOT NULL,<br />
[Size] [nvarchar](5) NULL,<br />
[SizeUnitMeasureCode] [nchar](3) NULL,<br />
[WeightUnitMeasureCode] [nchar](3) NULL,<br />
[Weight] [decimal](8, 2) NULL,<br />
[DaysToManufacture] [int] NOT NULL,<br />
[ProductLine] [nchar](2) NULL,<br />
[Class] [nchar](2) NULL,<br />
[Style] [nchar](2) NULL,<br />
[ProductSubcategoryID] [int] NULL,<br />
[ProductModelID] [int] NULL,<br />
[SellStartDate] [datetime] NOT NULL,<br />
[SellEndDate] [datetime] NULL,<br />
[DiscontinuedDate] [datetime] NULL,<br />
[rowguid] [uniqueidentifier] ROWGUIDCOL NOT NULL,<br />
[ModifiedDate] [datetime] NOT NULL<br />
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]<br />
GO<br />
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&#8217;Adjustable Race&#8217;, N&#8217;AR-5381&#8242;, 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&#8217;694215b7-08f7-4c0d-acb1-d734ba44c0c8&#8242;, CAST(0x00009A5C00A53CF8 AS DateTime))<br />
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&#8217;Bearing Ball&#8217;, N&#8217;BA-8327&#8242;, 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&#8217;58ae3c20-4f3a-4749-a7d4-d568806cc537&#8242;, CAST(0x00009A5C00A53CF8 AS DateTime))<br />
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&#8217;BB Ball Bearing&#8217;, N&#8217;BE-2349&#8242;, 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&#8217;9c21aed2-5bfa-4f18-bcb8-f11638dc2e4e&#8217;, CAST(0x00009A5C00A53CF8 AS DateTime))<br />
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&#8217;Headset Ball Bearings&#8217;, N&#8217;BE-2908&#8242;, 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&#8217;ecfed6cb-51ff-49b5-b06c-7d8ac834db8b&#8217;, CAST(0x00009A5C00A53CF8 AS DateTime))<br />
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&#8217;Blade&#8217;, N&#8217;BL-2036&#8242;, 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&#8217;e73e9750-603b-4131-89f5-3dd15ed5ff80&#8242;, CAST(0x00009A5C00A53CF8 AS DateTime))<br />
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&#8217;LL Crankarm&#8217;, N&#8217;CA-5965&#8242;, 0, 0, N&#8217;Black&#8217;, 500, 375, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 0, NULL, N&#8217;L &#8216;, NULL, NULL, NULL, CAST(0x0000921E00000000 AS DateTime), NULL, NULL, N&#8217;3c9d10b7-a6b2-4774-9963-c19dcee72fea&#8217;, CAST(0x00009A5C00A53CF8 AS DateTime))<br />
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&#8217;ML Crankarm&#8217;, N&#8217;CA-6738&#8242;, 0, 0, N&#8217;Black&#8217;, 500, 375, 0.0000, 0.0000, NULL, NULL, NULL, NULL, 0, NULL, N&#8217;M &#8216;, NULL, NULL, NULL, CAST(0x0000921E00000000 AS DateTime), NULL, NULL, N&#8217;eabb9a92-fa07-4eab-8955-f0517b4a4ca7&#8217;, CAST(0x00009A5C00A53CF8 AS DateTime))<br />
[/sql]</p>
<h3>Create application</h3>
<p>Our database is ready, now create a Web API application in visual studio and then entity with the above mentioned database.</p>
<div id="attachment_11556" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Creating-Entity-e1463031313825.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-11556" class="size-large wp-image-11556" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Creating-Entity-1024x542.png" alt="Creating Entity" width="634" height="336" /></a><p id="caption-attachment-11556" class="wp-caption-text">Creating Entity</p></div>
<p>If you don&#8217;t know how to create an entity in your solution, please read that <a href="http://sibeeshpassion.com/web-api-with-angular-js/" target="_blank" rel="noopener">here</a>. I have mentioned the steps to be followed in that article. Once you have created the entity, you are good to go and create a 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.</p>
<div id="attachment_11557" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Web-API-2-Controller-with-actions-using-Entity-Framework-e1463031653319.png"><img decoding="async" aria-describedby="caption-attachment-11557" class="size-full wp-image-11557" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Web-API-2-Controller-with-actions-using-Entity-Framework-e1463031653319.png" alt="Web API 2 Controller with actions, using Entity Framework" width="650" height="457" srcset="/wp-content/uploads/2016/05/Web-API-2-Controller-with-actions-using-Entity-Framework-e1463031653319.png 650w, /wp-content/uploads/2016/05/Web-API-2-Controller-with-actions-using-Entity-Framework-e1463031653319-300x211.png 300w, /wp-content/uploads/2016/05/Web-API-2-Controller-with-actions-using-Entity-Framework-e1463031653319-400x281.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11557" class="wp-caption-text">Web API 2 Controller with actions, using Entity Framework</p></div>
<p>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.</p>
<p>[csharp]<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Data;<br />
using System.Data.Entity;<br />
using System.Data.Entity.Infrastructure;<br />
using System.Linq;<br />
using System.Net;<br />
using System.Net.Http;<br />
using System.Web.Http;<br />
using System.Web.Http.Description;<br />
using ControllerActionDescriptions.Models;</p>
<p>namespace ControllerActionDescriptions.Controllers<br />
{<br />
public class ProductsController : ApiController<br />
{<br />
private TrialsDBEntities db = new TrialsDBEntities();<br />
#region GetProducts<br />
/// &lt;summary&gt;<br />
/// Get all the products available<br />
/// GET: api/Products<br />
/// &lt;/summary&gt;<br />
public IQueryable&lt;Product&gt; GetProducts()<br />
{<br />
return db.Products;<br />
}<br />
#endregion</p>
<p>#region GetProductWithParameter<br />
/// &lt;summary&gt;<br />
/// Get a single product by id<br />
/// GET: api/Products/5<br />
/// &lt;param name=&#8221;id&#8221;&gt;&lt;/param&gt;<br />
/// &lt;/summary&gt;<br />
[ResponseType(typeof(Product))]<br />
public IHttpActionResult GetProduct(int id)<br />
{<br />
Product product = db.Products.Find(id);<br />
if (product == null)<br />
{<br />
return NotFound();<br />
}</p>
<p>return Ok(product);<br />
}<br />
#endregion<br />
// PUT: api/Products/5<br />
[ResponseType(typeof(void))]<br />
public IHttpActionResult PutProduct(int id, Product product)<br />
{<br />
if (!ModelState.IsValid)<br />
{<br />
return BadRequest(ModelState);<br />
}</p>
<p>if (id != product.ProductID)<br />
{<br />
return BadRequest();<br />
}</p>
<p>db.Entry(product).State = EntityState.Modified;</p>
<p>try<br />
{<br />
db.SaveChanges();<br />
}<br />
catch (DbUpdateConcurrencyException)<br />
{<br />
if (!ProductExists(id))<br />
{<br />
return NotFound();<br />
}<br />
else<br />
{<br />
throw;<br />
}<br />
}</p>
<p>return StatusCode(HttpStatusCode.NoContent);<br />
}</p>
<p>// POST: api/Products<br />
[ResponseType(typeof(Product))]<br />
public IHttpActionResult PostProduct(Product product)<br />
{<br />
if (!ModelState.IsValid)<br />
{<br />
return BadRequest(ModelState);<br />
}</p>
<p>db.Products.Add(product);</p>
<p>try<br />
{<br />
db.SaveChanges();<br />
}<br />
catch (DbUpdateException)<br />
{<br />
if (ProductExists(product.ProductID))<br />
{<br />
return Conflict();<br />
}<br />
else<br />
{<br />
throw;<br />
}<br />
}</p>
<p>return CreatedAtRoute(&#8220;DefaultApi&#8221;, new { id = product.ProductID }, product);<br />
}</p>
<p>// DELETE: api/Products/5<br />
[ResponseType(typeof(Product))]<br />
public IHttpActionResult DeleteProduct(int id)<br />
{<br />
Product product = db.Products.Find(id);<br />
if (product == null)<br />
{<br />
return NotFound();<br />
}</p>
<p>db.Products.Remove(product);<br />
db.SaveChanges();</p>
<p>return Ok(product);<br />
}</p>
<p>protected override void Dispose(bool disposing)<br />
{<br />
if (disposing)<br />
{<br />
db.Dispose();<br />
}<br />
base.Dispose(disposing);<br />
}</p>
<p>private bool ProductExists(int id)<br />
{<br />
return db.Products.Count(e =&gt; e.ProductID == id) &gt; 0;<br />
}<br />
}<br />
}<br />
[/csharp]</p>
<h2><strong>Installing WebApiTestClient</strong></h2>
<p>To install the package, please go to your Package Manage Console from NuGet Package Manager and run the following command.</p>
<p>[csharp]<br />
Install-Package WebApiTestClient<br />
[/csharp]</p>
<p>You can always get the details about the package <a href="https://www.nuget.org/packages/WebApiTestClient" target="_blank" rel="noopener">here</a>.</p>
<p>Once you install the package, you can see some files are added to your Script and Area folder as preceding.</p>
<div id="attachment_11655" style="width: 291px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Script-and-Area-Folder.png"><img decoding="async" aria-describedby="caption-attachment-11655" class="size-full wp-image-11655" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Script-and-Area-Folder.png" alt="Script and Area Folder" width="281" height="780" srcset="/wp-content/uploads/2016/05/Script-and-Area-Folder.png 281w, /wp-content/uploads/2016/05/Script-and-Area-Folder-108x300.png 108w, /wp-content/uploads/2016/05/Script-and-Area-Folder-216x600.png 216w" sizes="(max-width: 281px) 100vw, 281px" /></a><p id="caption-attachment-11655" class="wp-caption-text">Script and Area Folder</p></div>
<h2><strong>Configuring WebApiTestClient</strong></h2>
<p>To configure the WebApiTestClient, please go to the folder Areas-&gt;Views-&gt;Help and then click on Api.cshtml</p>
<div id="attachment_11656" style="width: 272px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Api-Cshtml.png"><img decoding="async" aria-describedby="caption-attachment-11656" class="size-full wp-image-11656" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Api-Cshtml.png" alt="Api Cshtml" width="262" height="232" /></a><p id="caption-attachment-11656" class="wp-caption-text">Api Cshtml</p></div>
<p>This is the view shown when you click on each API in your help page. Now add the preceding code block to that view.</p>
<p>[html]<br />
@Html.DisplayForModel(&#8220;TestClientDialogs&#8221;)<br />
@section Scripts<br />
{<br />
&lt;link href=&#8221;~/Areas/HelpPage/HelpPage.css&#8221; rel=&#8221;stylesheet&#8221; type=&#8221;text/css&#8221; /&gt;<br />
@Html.DisplayForModel(&#8220;TestClientReferences&#8221;)<br />
}<br />
[/html]</p>
<div id="attachment_11657" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Code-Block-e1464327127249.png"><img decoding="async" aria-describedby="caption-attachment-11657" class="size-full wp-image-11657" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Code-Block-e1464327127249.png" alt="Code Block" width="650" height="503" srcset="/wp-content/uploads/2016/05/Code-Block-e1464327127249.png 461w, /wp-content/uploads/2016/05/Code-Block-e1464327127249-300x232.png 300w, /wp-content/uploads/2016/05/Code-Block-e1464327127249-400x310.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11657" class="wp-caption-text">Code Block</p></div>
<h2><strong>Testing WebApiTestClient</strong></h2>
<p>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.</p>
<div id="attachment_11658" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-API-Client-Output-e1464327451839.png"><img decoding="async" aria-describedby="caption-attachment-11658" class="size-large wp-image-11658" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-API-Client-Output-1024x451.png" alt="Test API Client Output" width="634" height="279" /></a><p id="caption-attachment-11658" class="wp-caption-text">Test API Client Output</p></div>
<p>Now if you send your request by clicking the send button, you will get an output as follows.</p>
<div id="attachment_11659" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-API-Client-Output-With-Response-e1464327640682.png"><img decoding="async" aria-describedby="caption-attachment-11659" class="size-full wp-image-11659" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-API-Client-Output-With-Response-e1464327640682.png" alt="Test API Client Output With Response" width="650" height="484" srcset="/wp-content/uploads/2016/05/Test-API-Client-Output-With-Response-e1464327640682.png 479w, /wp-content/uploads/2016/05/Test-API-Client-Output-With-Response-e1464327640682-300x223.png 300w, /wp-content/uploads/2016/05/Test-API-Client-Output-With-Response-e1464327640682-400x298.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11659" class="wp-caption-text">Test API Client Output With Response</p></div>
<p>You can always give id parameter as follows.</p>
<div id="attachment_11660" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-Client-With-Parameters-e1464327873548.png"><img decoding="async" aria-describedby="caption-attachment-11660" class="size-full wp-image-11660" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-Client-With-Parameters-e1464327873548.png" alt="Test Client With Parameters" width="650" height="302" srcset="/wp-content/uploads/2016/05/Test-Client-With-Parameters-e1464327873548.png 650w, /wp-content/uploads/2016/05/Test-Client-With-Parameters-e1464327873548-300x139.png 300w, /wp-content/uploads/2016/05/Test-Client-With-Parameters-e1464327873548-400x186.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11660" class="wp-caption-text">Test Client With Parameters</p></div>
<p>You can also give content-length and content-type in your post request as follows.</p>
<div id="attachment_11661" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-Client-With-Post-e1464328070355.png"><img decoding="async" aria-describedby="caption-attachment-11661" class="size-full wp-image-11661" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-Client-With-Post-e1464328070355.png" alt="Test Client With Post" width="650" height="468" srcset="/wp-content/uploads/2016/05/Test-Client-With-Post-e1464328070355.png 496w, /wp-content/uploads/2016/05/Test-Client-With-Post-e1464328070355-300x216.png 300w, /wp-content/uploads/2016/05/Test-Client-With-Post-e1464328070355-400x288.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11661" class="wp-caption-text">Test Client With Post</p></div>
<div id="attachment_11662" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-Client-With-PUT-Request-e1464328191663.png"><img decoding="async" aria-describedby="caption-attachment-11662" class="size-full wp-image-11662" src="http://sibeeshpassion.com/wp-content/uploads/2016/05/Test-Client-With-PUT-Request-e1464328191663.png" alt="Test Client With PUT Request" width="650" height="542" srcset="/wp-content/uploads/2016/05/Test-Client-With-PUT-Request-e1464328191663.png 428w, /wp-content/uploads/2016/05/Test-Client-With-PUT-Request-e1464328191663-300x250.png 300w, /wp-content/uploads/2016/05/Test-Client-With-PUT-Request-e1464328191663-400x334.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11662" class="wp-caption-text">Test Client With PUT Request</p></div>
<h2><strong>References</strong></h2>
<ul>
<li><a href="https://blogs.msdn.microsoft.com/yaohuang1/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page/" target="_blank" rel="noopener">Sample Test Client</a></li>
<li><a href="https://www.nuget.org/packages/WebApiTestClient" target="_blank" rel="noopener">WebApiTestClient</a></li>
</ul>
<p>Author has already posted the source code in GitHub, please check <a href="https://github.com/yaohuang/WebApiTestClient" target="_blank" rel="noopener">here</a>.</p>
<p>Have a happy coding!.</p>
<h2><strong>Conclusion</strong></h2>
<p>Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h2><strong>Your turn. What do you think?</strong></h2>
<p>A blog isn&#8217;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.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/working-with-test-client-in-asp-net-web-api-help-page/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
