如何将IHttpHandler和IHttpModule迁移到ASP.NET Core中间件
ASP.NET Core是一个跨平台、开源的框架,用于在Windows、Mac和Linux操作系统(OS)上开发web应用程序。你可以使用以下任何IDE开发ASP.NET Core 应用程序:
- Visual Studio
- Visual Studio for Mac
- Visual Studio Code
在这篇博文中,我们将学习如何如何将asp.net IHttpHandler和IHttpModule迁移到ASP.NET Core中间件并提供代码示例。
让我们开始吧!
ASP.NET IHttpHandler
在ASP.NET应用程序中,HTTP处理程序是一个进程,它在对向web服务器的每个响应上执行。我们可以创建自己的自定义HTTP处理程序。
下面是将所有.aspx页重定向到一个新页的代码。
public class RedirectionHandler : IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
var response = context.Response;
response.Write("<p>Process files with .aspx extension</p>");
// Any redirection logic can be written here.
}
}
web.config中添加如下代码:
<add name="RedirectionHandler" verb="*" path="*.aspx" type="MyWebApplication.RedirectionHandler" resourceType="Unspecified"/>
ASP.NET IHTTPModule
IHttpModule还将在应用程序的每个请求的HTTP处理程序执行之前和之后。它们帮助我们验证传入和传出的请求并修改它们。
下面是用于根据用户的IP地址限制用户的IHttpModule代码。
public class IPRestrictionModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (source, arguments) =>
{
var application = (HttpApplication)source;
var beginContext = application.Context;
beginContext.Response.Write("<p>Restrict Users based on IP</p>"); // Code logic comes here.
}; context.EndRequest += (source, arguments) =>
{
var application = (HttpApplication)source;
var endContext = application.Context;
endContext.Response.Write("<p>Request ended.</p>");
};
}
}
web.config中添加如下代码:
<add name="RestrictionModule" type=" MyWebApplication.IPRestrictionModule" />
ASP.NET Core中间件
在ASP.NET Core应用程序中,中间件组件将替换IHttpHandler和IHttpModule。它是针对每个请求执行的组件。我们可以使用IApplicationBuilder接口在Startup类的Configure方法中添加中间件。
可以使用以下四种方法:
Run |
终止HTTP管道。 |
Use |
将中间件添加到请求管道。 |
Map |
根据请求路径匹配请求委托 |
MapWhen |
支持基于谓词的中间件分支。 |
让我们看看如何将ASP.NET IHttpHandler和IHttpModule迁移到ASP.NET Core中间件!
将 IHttpHandler迁移到ASP.NET Core中间件
1. 使用如下代码创建RedirectionHandlerMiddleware 类
public class RedirectionHandlerMiddleware
{
private RequestDelegate _next;
public RedirectionHandlerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
await context.Response.WriteAsync("<p>Process files with .aspx extension</p>");
// Any Redirection logic can be return here.
}
}
2. 在ApplicationBuilder中创建一个扩展方法,以在请求管道中使用RedirectionHandlerMiddleware。
3. 然后,为扩展方法创建一个名为MiddlewareExtension的类,并在其中使用以下代码。
public static class MiddlewareExtension
{
public static IApplicationBuilder UseRedirectionHanlderMiddleware
(this IApplicationBuilder applicationBuilder)
{
return applicationBuilder.UseMiddleware<RedirectionHandlerMiddleware>();
}
}
4. 我们需要在Startup.cs文件中包含下一个代码。
app.MapWhen(context => context.Request.Path.ToString().EndsWith(".aspx"),
appBuilder => {
appBuilder.UseRedirectionHanlderMiddleware();
});
现在,我们已经完成了IHttpHandler的迁移。
将 IHttpModule迁移到ASP.NET Core中间件
1. 使用如下代码创建IPRestrictionModuleMiddleware类。
public class IPRestrictionModuleMiddleware
{
private RequestDelegate _next;
public IPRestrictionModuleMiddleware (RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
await context.Response.WriteAsync("<p>Begin request</p>");
await _next.Invoke(context);
await context.Response.WriteAsync("<p>End request</p>");
}
}
2. 与之前一样,我们需要添加一个扩展方法用来在请求管道中添加中间件。
3. 然后,向现有的MiddlewareExtension类中添加以下代码:
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseRedirectionHanlderMiddleware
(this IApplicationBuilder applicationBuilder)
{
return applicationBuilder.UseMiddleware<RedirectionHandlerMiddleware>();
} public static IApplicationBuilder UseIPRestrictionModuleMiddleware
(this IApplicationBuilder builder)
{
return builder.UseMiddleware<IPRestrictionModuleMiddleware>();
}
}
4. 然后,将中间件包含在Startup.cs文件中。
// For Module
app.UseIPRestrictionModuleMiddleware(); // For Handler
app.MapWhen(context => context.Request.Path.ToString().EndsWith(".aspx"),
appBuilder => {
appBuilder.UseRedirectionHanlderMiddleware();
});
这样,我们就完成了对IHttpModule的迁移。
最新文章
- 使用游戏引擎photon打造一款特殊的远程控制软件
- jsonP跨域调用
- 【Discuz】-QQ互联登陆提示错误信息:Unknown column &#39;conuintoken&#39; in &#39;field list&#39;
- Android错误:W/ResourceType(2411): No package identifier when getting value for resource number 0x
- mac os安装基本的install环境,命令行安装软件
- java 集合(Map3)
- 菜鸟-手把手教你把Acegi应用到实际项目中(5)
- 驱动笔记 - file_operations
- Wordpress 网站搭建及性能监控方法详解!
- 【高精度】Vijos P1010 清帝之惑之乾隆
- fstream读写UNICODE文件
- [精华]Hadoop,HBase分布式集群和solr环境搭建
- line-height系列(二)——对行内元素(文字、图片、兄弟元素)、块级元素设置line-height后的表现
- 关于AJAX异步请求
- Learning-Python【21】:Python常用模块(4)—— re、logging、hashlib、subprocess
- Linux之删除带有空格的文件(而不是目录)
- 【进阶修炼】&mdash;&mdash;改善C#程序质量(9)
- Go之获取Windows下文件是否隐藏
- RESTful api架构设计
- ios开发之--理解NSStringDrawingOptions每个选项的用法与意义
热门文章
- Linux 防火墙相关操作
- 十四自定义构建购物计算组件&;表单组件
- Codeforces Round #631 (Div. 2)
- Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)【ABCD】
- 【noi 2.6_8787】数的划分(DP){附【转】整数划分的解题方法}
- Kubernets二进制安装(18)之安装部署Heapster
- HTTP 请求过程以及报文结构
- 1.搭建NFS环境,用于存储数据
- 快速搞懂.NET 5/.NET Core应用程序的发布部署
- 十三香 &; 香料