关于c#:如何将IHttpHandler和IHttpModule迁移到ASPNET-Core中间件

2次阅读

共计 3668 个字符,预计需要花费 10 分钟才能阅读完成。

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.
 }
}
  1. 在 ApplicationBuilder 中创立一个扩大办法,以在申请管道中应用 RedirectionHandlerMiddleware。
  2. 而后,为扩大办法创立一个名为 MiddlewareExtension 的类,并在其中应用以下代码。​​​​​​​
public static class MiddlewareExtension     
{
     public static IApplicationBuilder UseRedirectionHanlderMiddleware
                   (this IApplicationBuilder applicationBuilder)
     {return applicationBuilder.UseMiddleware<RedirectionHandlerMiddleware>();
     }
}
  1. 咱们须要在 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>");
       }
 }
  1. 与之前一样,咱们须要增加一个扩大办法用来在申请管道中增加中间件。
  2. 而后,向现有的 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>();
    }
}
  1. 而后,将中间件蕴含在 Startup.cs 文件中。​​​​​​​
// For Module
app.UseIPRestrictionModuleMiddleware();
 
 // For Handler
app.MapWhen(context => context.Request.Path.ToString().EndsWith(".aspx"),
        appBuilder => {appBuilder.UseRedirectionHanlderMiddleware();
         });

这样,咱们就实现了对 IHttpModule 的迁徙。

原文链接:https://www.red-gate.com/simp…

正文完
 0