
- 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 to add custom message handlers to the pipeline in Asp.Net webAPI C#?
To create a custom Server-Side HTTP Message Handler in ASP.NET Web API, we need to create a class that must be derived from the System.Net.Http.DelegatingHandler.
Step 1 −
Create a controller and its corresponding action methods.
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; } } }
Step 2 −
Create our own CutomerMessageHandler class.
Example
using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; namespace DemoWebApplication{ public class CustomMessageHandler : DelegatingHandler{ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken){ var response = new HttpResponseMessage(HttpStatusCode.OK){ Content = new StringContent("Result through custom message handler..") }; var taskCompletionSource = new TaskCompletionSource<HttpResponseMessage>(); taskCompletionSource.SetResult(response); return await taskCompletionSource.Task; } } }
We have declared the CustomMessageHandler class derived from DelegatingHandler and within that we have overriden the SendAsync() function.
When a HTTP request arrives CustomMessageHandler will execute and it will return one HTTP message on its own, without processing the HTTP request further. So ultimately we are preventing each and every HTTP request from reach its higher level.
Step 3 −
Now register the CustomMessageHandler in Global.asax class.
public class WebApiApplication : System.Web.HttpApplication{ protected void Application_Start(){ GlobalConfiguration.Configure(WebApiConfig.Register); GlobalConfiguration.Configuration.MessageHandlers.Add(new CustomMessageHandler()); } }
Step 4 −
Run the application and provide the Url.
From the above output we could see the message that we have set in our CustomMessageHandler class. So the HTTP message is not reaching the Get() action and before that it is returning to our CustomMessageHandler class.
- Related Articles
- What are built-in message handlers in Asp.Net webAPI C#?
- How to do versioning with custom media type in C# ASP.NET WebAPI?
- How to return custom result type from an action method in C# ASP.NET WebAPI?
- How to resolve CORS issue in C# ASP.NET WebAPI?
- How to configure C# ASP.NET WebAPI in web.configure file?
- How to do versioning with the Querystring parameter in C# ASP.NET WebAPI?
- How to do versioning with accept header in C# ASP.NET WebAPI?
- How can we test C# Asp.Net WebAPI?
- How to do Web API versioning with URI in C# ASP.NET WebAPI?
- How to consume Asp.Net WebAPI endpoints from other applications using C#?
- How do we specify MIME type in Asp.Net WebAPI C#?
- What is Content Negotiation in Asp.Net webAPI C#?
- What is parameter binding in C# ASP.NET WebAPI?
- What is the usage of DelegatingHandler in Asp.Net webAPI C#?
- What are the advantages of using C# ASP.NET WebAPI?
