OWIN Middleware Using C# .NET

Introduction:

Wikipedia states:

Middleware is computer software that provides services to software applications beyond those available from the operating system. It can be described as "software glue". Middleware makes it easier for software developers to perform communication and input/output, so they can focus on the specific purpose of their application.

Middleware is the software that connects software components or enterprise applications. Middleware is the software layer that lies between the operating system and the applications on each side of a distributed computer network.

Typically, it supports complex, distributed business software applications.

Middleware includes Web servers, application servers, content management systems, and similar tools that support application development and delivery. It is especially integral to information technologies.

Conceptual View:

view

Typically middleware plays a vital role in the request and response designing. Middleware can modify your request and send it to the server. So in this article we will try to develop sample middleware and check how to Post, Get values.

Requirements:

usingMicrosoft.Owin;


Code Snippet for Sample Middleware
  1. public class Middleware < TModelType > : OwinMiddlewarewhereTModelType: classnew()  
  2. {  
  3.     public Middleware(OwinMiddleware next): base(next)  
  4.     {  
  5.   
  6.     }  
  7.   
  8.     public override Task Invoke(IOwinContext context)  
  9.     {  
  10.   
  11.         return this.Next.Invoke(context);  
  12.   
  13.     }  
  14. }  
The following code helps you to connect this middle ware snippet to your class:
  1. public void Configuration(IAppBuilder builder)  
  2. {  
  3.   
  4.     builder.Use < Middleware < RequestHandler >> ();  
  5.   
  6.     // It act as Controller  
  7.   
  8.     builder.Map("/Step1", config =>  
  9.     {  
  10.   
  11.         config.Use((context, next) => context.Response.WriteAsync("Response Receive from your own middleware "));  
  12.   
  13.     });  
  14. }  
Middleware passes your request to controller. Let’s take a look to start and test API,
  1. try   
  2. {  
  3.     using(WebApp.Start < StartUpController > ("http://localhost:5888/"))  
  4.     {  
  5.         Console.WriteLine("Now you can start working on your OWN API");  
  6.         Console.ReadLine();  
  7.     }  
  8. catch (Exception ex)  
  9. {  
  10.     Console.WriteLine(ex.Message.ToString());  
  11.     Console.Read();  
  12. }  
Use Postman to Test your API,

api

Now take a look for post.

Making an API Request to post operation with POSTMAN:

postman

Break Point step IN.

code

Text Visualization for Request:

Request

Code for Builder to use POST operations, 
  1. // Post Operations  
  2. builder.Use((context, next) =>  
  3. {  
  4.     if (context.Request.Method == "POST")  
  5.     {  
  6.         var form = context.Get < RequestHandler > ("Body");  
  7.         // return context.Response.WriteAsync("Post Received :" + form.value);  
  8.         returncontext.Response.WriteAsync("Post Received :");  
  9.     }  
  10.     return next();  
  11. });  
Download the source code from OWINSOURCEMIDDLEWARE for c#.net.

References and for more information: 
Read more articles on C# Programming: