Core Middleware
Core Middleware
C# - Extension Method
Extension methods are additional methods.
using System;
namespace ExtensionMethod {
class Sample{
public void M1() { Console.WriteLine("Method Name: M1"); }
public void M2() { Console.WriteLine("Method Name: M2");}
public void M3(){ Console.WriteLine("Method Name: M3");}
}
}
C# - Extension Method
Now we create a static class named as NewMethodClass
in SampleEx.cs file. It contains two methods that are
M4() and M5().
using System;
namespace NewMethodClass {
class SampleEx{
public static void M4(this Sample s)
{
Console.WriteLine("Method Name: M4");
}
public static void M5(this Sample s, string str)
{
Console.WriteLine(str);
}
}
}
C# - Extension Method
Inside Main Method
app.Run(MyMiddleware);
Method Signature
public static void Run(this IApplicationBuilder ap
RequestDelegate handler)
• RequestDelegate ? It is a delegate
public delegate Task RequestDelegate (HttpContext context);
• HttpContext
It is HttpRequest http://Controller//ActionM ..
Understand Run Method
RequestDelegate signature.
public delegate Task RequestDelegate(HttpContext context);
Understanding Run Method
public void Configure(IApplicationBuilder app)
{
app.Run(MyMiddleware);
}
app.Run(MyMiddleware);
Lambda Expression
The most important point that you need to keep in mind is the order in
which the middleware components are added in the Configure method of
the Startup class defines the order in which these middleware
components are going to be invoked on requests and the reverse order
for the response. So, the order is critical for defining the security,
performance, and functionality of the application.
Static Files Middleware
ASP.NET Core application cannot serve static
files by default.
1.Default.html
2.Default.htm
3.Index.html
4.Index.htm
The Microsoft.AspNetCore.Diagnostics packag
e includes following extension methods to
handle exceptions in different scenario:
1. UseDeveloperExceptionPage
2. UseExceptionHandler
UseDeveloperExceptionPage
The UseDeveloperExceptionPage extension
method adds middleware into the request
pipeline.