应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

ASP.NET管道模型之HttpModule

6344人阅读 2019/8/14 11:21 总访问:5182418 评论:2 收藏:0 手机
分类: .NET


HttpModule是什么

在Http请求处理过程中,请求会前后两次通过一系列的HttpModule,这些Module对Http请求具有完全控制权,可以分别实现某个工作前的事情和某个工作后的事情。有点类似net core中的中间件,可以用来做权限控制,依赖注入,session维护,自定义路由等各种操作。httpmodule在管道模型之中很重要,如果我们去看mvc的源码,会发现里边大量的使用到了它


初识一个最简单的HttpModule

首先创建一个模块后,模块本身是不会被执行的,我们需要在web.config中进行配置

  1.   <system.webServer>
  2.     <modules>
  3.       <add name="checkLoginModule" type="WebApplication2.MyModule1"/>
  4.     </modules>
  5.   </system.webServer>

其中name就是模块的标识名字可以随便取只要不重名就好了,后面type就是模块对应的类型,运行的时候asp.net管道会取出来通过反射创建对象进行注入,这里配置的方式要注意经典模式与集成模式。


然后在模块代码中随便注册两个事件输出一句话:

  1. public void Init(HttpApplication context)
  2.         {
  3.             // 下面是如何处理 LogRequest 事件并为其 
  4.             // 提供自定义日志记录实现的示例
  5.             context.LogRequest += new EventHandler(OnLogRequest);
  6.             context.BeginRequest += context_BeginRequest;
  7.             context.AcquireRequestState += context_AcquireRequestState;
  8.         }
  9.         void context_BeginRequest(object sender, EventArgs e)
  10.         {
  11.             HttpApplication context = sender as HttpApplication;
  12.             context.Response.Write("exec_context_BeginRequest<br/>");
  13.         }
  14.         void context_AcquireRequestState(object sender, EventArgs e)
  15.         {
  16.             HttpApplication context = sender as HttpApplication;
  17.             context.Response.Write("exec_context_AcquireRequestState<br/>");
  18.         }

效果如下:

这样配置方式,css,img这些静态资源也会进入模块里边,如果不想这些静态资源进入只需加一句配置:

preCondition="managedHandler"

  1.   <system.webServer>
  2.     <modules>
  3.       <add name="checkLoginModule" type="WebApplication2.MyModule1" preCondition="managedHandler"/>
  4.     </modules>
  5.   </system.webServer>

如果我们想要验证一点权限的话就很重要,拿到请求的页面以及登录信息,验证一下是否有该地方的访问权限即可


asp.net本身自带了许多HttpModule

在文件夹C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config中找到web.config:

打开后找到httpModules就可以看到自带的httpmodule:

  1. <httpModules>
  2.     <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
  3.     <add name="Session" type="System.Web.SessionState.SessionStateModule" />
  4.     <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
  5.     <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
  6.     <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />
  7.     <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
  8.     <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
  9.     <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />
  10.     <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />
  11.     <add name="Profile" type="System.Web.Profile.ProfileModule" />
  12.     <add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  13.     <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  14.     <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
  15.     <add name="ScriptModule-4.0" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  16. </httpModules>


使用代码获取系统使用的所有模块

asp.net中获取(mvc获取类似):

  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             //获取所有模块的名称
  4.             string[] allKeys = HttpContext.Current.ApplicationInstance.Modules.AllKeys;
  5.             Response.Write("<table class='mytable'>");
  6.             Response.Write("<tr class='header'><td>模块名称</td><td>模块类型</td></tr>");
  7.             foreach (string item in allKeys)
  8.             {
  9.                 //获取模块类型名称
  10.                 string typename = HttpContext.Current.ApplicationInstance.Modules[item].ToString();
  11.                 Response.Write("<tr><td>" + item + "</td><td>" + typename + "</td></tr>");
  12.             }
  13.             Response.Write("</table>");
  14.         }

效果如下:

其中的checkLoginModule就是自己自定义的那个

未完待续.........


少侠,我看你气宇不凡天赋异禀,这么帅,关注一波在走呗




欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

尘叶心繁

2019/8/14 13:08:47

白嫖路过

剑轩:@尘叶心繁白嫖一时爽,一直白嫖一直爽!

2019/8/14 13:54:20 回复

BadRequestObjectResult writing value of type 'Microsoft.ASPnetcore.Mvc.ValidationProblemDetails'。net core获取参数验证的错误。找到实体验证的错误

net core获取参数验证的错误,这样写就可以了: services.Configure&lt;ApiBehaviorOptions&gt;(options =&gt; { opt...

dotnet-5.0.17 运行时,与ASPnetcore-5.0.17 运行时下载。ASPnetcore-runtime-5.0.17-win-x64。dotnet-runtime-5.0.17-win-x64。

https://dotnet.microsoft.com/zh-cn/download/dotnet/thank-you/runtime-aspnetcore-5.0.17-windows-x64-installer?cid=g...

HttpModule忽略静态资源

只需要配置一个preCondition=&quot;managedHandler&quot;即可&lt;system.webServer&gt; &lt;modules&gt; &lt;addname=&qu...

HttpModule管道模型小细节

//在HttpModule管道模型中,如果用重定向跳转页面,因为HttpModule是程序最开始的执行地方,所以在里面永远都是循环的在执...

net core 使用 EF Code First

下面这些内容很老了看这篇:https://www.tnblog.net/aojiancc2/article/details/5365 项目使用多层,把数据库访问...

cAPS.net 保存base64位格式的图片

publicvoidUpload() { //取出图片对应的base64位字符 stringimgBase=Request[&quot;imgBase&quot;]; //c#里边的base6...

Quartz.net实例动态改变周期调度。misfire、Cron

Quartz:Java编写的开源的任务调度作业框架 类似Timer之类定时执行的功能,但是更强大Quartz.NET:是把Quartz转成C# NuGet...

.net Windows服务发布、安装、卸载、监听脚本。服务调试

一、脚本 为方便不用每次都去写安装卸载的脚本1.安装脚本@echooff @echo开始安装【服务】 %SystemRoot%\Microsoft.NET\Fr...

c、VB.net中全角半角转换方法

///&lt;summary&gt; ///转全角的函数(SBCcase) ///&lt;/summary&gt; ///&lt;paramname=&quot;input&quot;&gt;任意字符串...

.net mvc分部页,.net core分部页

.net分部页的三种方式第一种:@Html.Partial(&quot;_分部页&quot;)第二种:@{ Html.RenderPartial(&quot;分部页&quot;);}...

C.net 配合小程序实现经过第三方服务器中转文件

某些时候,微信小程序前段上传文件的时候需要经过第三方服务器再将文件上传到客户的服务器;操作如下:1:(小程序内向中端服...

.net实现QQ邮箱发送邮件功能

1、微软已经帮我们封装好了发送邮件的类MailMessage,MailMessage类构造一些邮件信息,然后通过SmtpClient进行邮件发送。Mai...

StackExchange.Redis操作redis(net core支持)

官方git开源地址https://github.com/StackExchange/StackExchange.Redis官方文档在docs里边都是官方的文档通过nuget命令下...

windows 自带的netsh进行端口映射

使用netsh 把本地任意ip的25566端口 映射到192.168.81.234的25565端口netshinterfaceportproxyaddv4tov4listenaddress=0.0....

确保.net程序始终以管理员身份运行

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; ...