如何将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的迁移。

如何将IHttpHandler和IHttpModule迁移到ASP.NET Core中间件的更多相关文章
- ASP.NET Core 中间件 自定义全局异常中间件以及 MVC异常过滤器作用
中间件是一种装配到应用管道以处理请求和响应的软件. 每个组件: 选择是否将请求传递到管道中的下一个组件. 可在管道中的下一个组件前后执行工作. 请求委托用于生成请求管道. 请求委托处理每个 HTTP ...
- 为什么你需要将代码迁移到ASP.NET Core 2.0?
随着 .NET Core 2.0 的发布,.NET 开源跨平台迎来了新的时代.开发者们可以选择使用命令行.个人喜好的文本编辑器.Visual Studio 2017 15.3 和 Visual Stu ...
- ASP.NET Core Identity 迁移数据 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core Identity 迁移数据 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 迁移数据 上一章节中我们配置了 ...
- 旧 WCF 项目迁移到 asp.net core + gRPC 的尝试
一个月前,公司的运行WCF的windows服务器down掉了,由于 AWS 没有通知,没有能第一时间发现问题. 所以,客户提出将WCF服务由C#改为JAVA,在Linux上面运行:一方面,AWS对Li ...
- ASP.NET Core 中间件Diagnostics使用
ASP.NET Core 中间件(Middleware)Diagnostics使用.对于中间件的介绍可以查看之前的文章ASP.NET Core 开发-中间件(Middleware). Diagnost ...
- ASP.NET Core 中间件Diagnostics使用 异常和错误信息
ASP.NET Core 中间件(Middleware)Diagnostics使用.对于中间件的介绍可以查看之前的文章ASP.NET Core 开发-中间件(Middleware). Diagnost ...
- [转帖]ASP.NET Core 中间件(Middleware)详解
ASP.NET Core 中间件(Middleware)详解 本文为官方文档译文,官方文档现已非机器翻译 https://docs.microsoft.com/zh-cn/aspnet/core/ ...
- ASP.NET Core中间件(Middleware)实现WCF SOAP服务端解析
ASP.NET Core中间件(Middleware)进阶学习实现SOAP 解析. 本篇将介绍实现ASP.NET Core SOAP服务端解析,而不是ASP.NET Core整个WCF host. 因 ...
- [转]ASP.NET Core 中间件详解及项目实战
本文转自:http://www.cnblogs.com/savorboard/p/5586229.html 前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际 ...
随机推荐
- HDU6331 Problem M. Walking Plan【Floyd + 矩阵 + 分块】
HDU6331 Problem M. Walking Plan 题意: 给出一张有\(N\)个点的有向图,有\(q\)次询问,每次询问从\(s\)到\(t\)且最少走\(k\)条边的最短路径是多少 \ ...
- c语言实现--顺序表操作
经过三天的时间终于把顺序表的操作实现搞定了.(主要是在测试部分停留了太长时间) 1;线性表顺序存储的概念:指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素. 2;采用的实现方式:一段地址连 ...
- k8s二进制部署 - 总结
镜像仓库: 安装软件:docker.docker-compose.harbor.nginx 1.下载cfssl.cfssljson.cfssl-certinfo,增加执行权限并放在PATH环境变量路径 ...
- map最最最基本用法
map<a,b>c中,a,b是变量类型 参数定义的map的名字 #include<stdio.h> #include<map> //头文件 map<int,c ...
- five86-1 (OpenNetadmin RCE+cp命令提权+crunch生成密码字典)
Vulnhub-Five86-1 实验环境 kali攻击机ip:192.168.56.116 Five86-1靶机ip:192.168.56.121 知识点及工具 nmap扫描 john爆破 Open ...
- 阿里巴巴java开发手册(2020版)
阿里巴巴java开发手册(2020版) 2020版 链接: pan.baidu.com/s/1Zls_FUBK- 密码: titz 2019版 链接: pan.baidu.com/s/1cvCVQvj ...
- git commit guidelines
git-commit-guidelines AngularJS Development Setup Running Tests Coding Rules Commit Message Guidelin ...
- webfullstack website
webfullstack website refs https://www.lanqiao.cn/paths/ xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许 ...
- codepen iframe theme id
codepen iframe theme id iframe css theme demos See the Pen css margin collapsing (1. 相邻兄弟元素) by xgqf ...
- 算法的时间复杂度 & 性能对比
算法的时间复杂度 & 性能对比 累加算法性能对比 // js 累加算法性能对比测试 const n = 10**6; (() => { console.time(`for`); let ...