
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How can we test C# Asp.Net WebAPI?
Testing WebApi involves sending a request and receiving the response. There are several ways to test the WebApi. Here we will test the WebApi using postman and swagger. Let us create a StudentController like below.
Student Model
namespace DemoWebApplication.Models{ public class Student{ public int Id { get; set; } public string Name { get; set; } } }
Student Controller
Example
using DemoWebApplication.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class StudentController : ApiController{ List<Student> students = new List<Student>{ new Student{ Id = 1, Name = "Mark" }, new Student{ Id = 2, Name = "John" } }; public IEnumerable<Student> Get(){ return students; } public Student Get(int id){ var studentForId = students.FirstOrDefault(x => x.Id == id); return studentForId; } } }
Test Using Swagger
Swagger is a specification for documenting REST API. It specifies the format (URL, method, and representation) to describe REST web services. The methods, parameters, and models description are tightly integrated into the server code, thereby maintaining the synchronization in APIs and its documentation.
In our application, using Manage Nuget packages install swagger.
Run our WebApi project and enter swagger/ui/index in the url.
The swagger will automatically list out the controller and its action method like below. We can expand the respective controller and test the endpoint using our request.
Get All Students Request
Get All Students Response
Get Students for Id Request
Get Students for Id Response
Test Using Postman
Postman is a popular API client that makes it easy for developers to create, share, test and document APIs. This is done by allowing users to create and save simple and complex HTTP/s requests, as well as read their responses. The result - more efficient and less tedious work. Postman can be installed as an application or can be send through browser like below.
Get All Students Request and Response
Get Students For Id Request and Response
- Related Articles
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- How to use ViewBag in ASP .Net MVC C#?
- What is ViewData in ASP .Net MVC C#?
- How can we test rust?
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- How can we assign alias names for the action method in C# ASP.NET WebAPI?
- How can we create a LOG filter for Logging purposes in C# ASP.NET WebAPI?
- If nuts contain fats, how can we test that?
- How can we create an exception filter to handle unhandled exceptions in C#\nASP.NET WebAPI?
- Can we test XPath expression in JSP?
- How do we specify MIME type in Asp.Net WebAPI C#?
- What are the three segments of the default route, that is present in ASP .Net MVC\nC#?
- How can we test for the existence of any record in MySQL subquery?
