ASP.NET IHttpModule IHttpHandler IHttpHandlerFactory 拦截请求
先来看看代码,拦截所有Http请求类。下面包含了两种类的集成 IHttpModule IHttpHandlerFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace SoonnetWebSite
{
/// <summary>
/// Handler2 的摘要说明
/// </summary>
public class Handler2 : IHttpModule
{
public void Dispose()
{
} private void Application_AcquireRequestState(Object source, EventArgs e)
{
HttpApplication httpApplication = (HttpApplication)source;
string url = httpApplication.Context.Request.Path.ToLower();
string imgPhysicalPath = httpApplication.Request.Url.ToString();
if (imgPhysicalPath.ToLower().IndexOf("https") != 0 && imgPhysicalPath.IndexOf("Video/VideoUpload.aspx") == -1 && imgPhysicalPath.IndexOf("Photo/PhotoUpload.aspx") == -1)
{
imgPhysicalPath = imgPhysicalPath.Replace("http", "https");
httpApplication.Response.Redirect(imgPhysicalPath);
return;
}
// httpApplication.Response.Redirect(imgPhysicalPath);
} public void Init(HttpApplication context)
{
context.AcquireRequestState += (new EventHandler(this.Application_AcquireRequestState));
}
//public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
//{
// IHttpHandler handler = null;
// string action = url.Substring(url.LastIndexOf("/", StringComparison.Ordinal) + 1);
// action = action.Substring(0, action.IndexOf(".", StringComparison.Ordinal));
// var Rurl = context.Request.RawUrl.Replace("/", ".");
// string actionClass = $"SoonnetWebSite.{Rurl}";
// if (true)
// { // } //} //public void ReleaseHandler(IHttpHandler handler)
//{
// throw new NotImplementedException();
//}
}
}
下面我们来看看IHttpHandler
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace SoonnetWebSite
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
private const string DEFAULTIMAGE_URL = "/SGL_Images/Userlogo_Big.jpg";
public void ProcessRequest(HttpContext context)
{
string imgPhysicalPath = context.Request.Url.ToString();
string rawUrl = context.Request.RawUrl.ToString();
System.Drawing.Image image = null;
if (File.Exists(context.Server.MapPath(rawUrl)))
{ //为空
image = System.Drawing.Image.FromFile(context.Server.MapPath(rawUrl));
}
else
{ //如果图片不存在,放上默认的图片
image = System.Drawing.Image.FromFile(context.Server.MapPath(DEFAULTIMAGE_URL));
}
//设置输出的类型
context.Response.ContentType = "image/jpeg";
//把图片保存到输出流里
image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
image.Dispose();
} public bool IsReusable
{
get
{
return false;
}
}
}
}
两端代码的配置如下
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1126400000" />
</requestFiltering>
</security>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="IHttpHandler" verb="GET,POST" path="LokARX.cc" type="SoonnetWebSite.Web_Data.Lok_ARequestX, SoonnetWebSite" />
<add name="IHttpHandler2" verb="GET" path="LokIF.cc" type="SoonnetWebSite.Web_Data.Lok_Interface, SoonnetWebSite" />
<add name="Handler1" verb="*" path="*.jpg" type="SoonnetWebSite.Handler1, SoonnetWebSite" />
</handlers>
<modules>
<add name="Handler2" type="SoonnetWebSite.Handler2,SoonnetWebSite" />
</modules>
</system.webServer>
发现个问题, IIS 配置的跟本机测试的节点不一样。线上的配置要用
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1126400000" />
</requestFiltering>
</security>
<validation validateIntegratedModeConfiguration="false" />
<HttpHandlers>
<add name="IHttpHandler" verb="GET,POST" path="LokARX.cc" type="SoonnetWebSite.Web_Data.Lok_ARequestX, SoonnetWebSite" />
<add name="IHttpHandler2" verb="GET" path="LokIF.cc" type="SoonnetWebSite.Web_Data.Lok_Interface, SoonnetWebSite" />
<add name="Handler1" verb="*" path="*.jpg" type="SoonnetWebSite.Handler1, SoonnetWebSite" />
</HttpHandlers>
<HttpModules>
<add name="Handler2" type="SoonnetWebSite.Handler2,SoonnetWebSite" />
</HttpModules>
</system.webServer>
前面配置多了一个http
是因为IIS 的经典模式跟集成模式的关系,
参考链接:https://blog.csdn.net/hongwei_23/article/details/44300923
http 转 https 参考链接:https://blog.csdn.net/suxuelian/article/details/80103514
ASP.NET IHttpModule IHttpHandler IHttpHandlerFactory 拦截请求的更多相关文章
- asp.net core 使用中间件拦截请求和返回数据,并对数据进行加密解密。
原文:asp.net core 使用中间件拦截请求和返回数据,并对数据进行加密解密. GitHub demo https://github.com/zhanglilong23/Asp.NetCore. ...
- Asp.Net 拦截请求自定义处理
需求: 在Aps.Net 应用中,对于浏览器请求的部分url的地址自定义处理,不交给路由系统或页面. 解决方案: 在全局文件Global.asax中 ,提供Application_BeginReque ...
- ASP.NET/MVC/Core的HTTP请求流程
ASP.NET HTTP管道(Pipeline)模型 1. 先讲一点,再深刻思考 一般我们都在写业务代码,优化页面,优化逻辑之间内徘徊.也许我们懂得HTTP,HTTPS的GET,POST,但是我们大部 ...
- 简述C#中IO的应用 RabbitMQ安装笔记 一次线上问题引发的对于C#中相等判断的思考 ef和mysql使用(一) ASP.NET/MVC/Core的HTTP请求流程
简述C#中IO的应用 在.NET Framework 中. System.IO 命名空间主要包含基于文件(和基于内存)的输入输出(I/O)服务的相关基础类库.和其他命名空间一样. System.I ...
- ASP.NET MVC学前篇之请求流程
ASP.NET MVC学前篇之请求流程 请求流程描述 对于请求的流程,文章的重点是讲HttpApplication和HttpModule之间的关系,以及一个简单的示例实现.(HttpModule又是M ...
- Asp.Net MVC是否针对每次请求都重新创建一个控制器实例
一.Asp.Net MVC是否针对每次请求都重新创建一个控制器实例 默认情况下,答案是确定的. ControllerBuilder类 ControllerBuilder.Current用户获取默认的控 ...
- webapi拦截请求
[AttributeUsage(AttributeTargets.Method)] public class WebApiSensitive : ActionFilterAttribute { pub ...
- Asp.net 4.0,首次请求目录下的文件时响应很慢
原文:Asp.net 4.0,首次请求目录下的文件时响应很慢 1. 问题起因2. 尝试过的处理思路3. 解决方法 1. 问题起因 一个从VS2003(.Net Framework 1.1)升级到.ne ...
- Asp.net Core 入门实战 2.请求流程
Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个源码合集,方便一次性Clone,喜欢的(Star),本系列持续更新,也可以通过我的网站访问 ...
随机推荐
- 使用apache commons net进行ftp传输
apache commons net的maven地址: http://mvnrepository.com/artifact/commons-net/commons-net/3.6 <!-- ht ...
- 提示/bin/roslyn/csc.exe权限不足
给/bin/roslyn/csc.exe 读取/执行 权限
- 集群架构01.Nginx初步安装配置
1.切换163yum 源,环境介绍 [root@moban ~]# cat /etc/redhat-release CentOS release 6.5 (Final) mv CentOS-Base. ...
- appium(toast处理)
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expec ...
- 最新 易车java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.易车等10家互联网公司的校招Offer,因为某些自身原因最终选择了易车.6.7月主要是做系统复习.项目复盘.LeetCode ...
- [mysql] C++操作mysql方法
下载:http://mirrors.sohu.com/mysql/MySQL-5.5/ From: http://www.cnblogs.com/magicsoar/p/3817518.html C+ ...
- hdoj3586 (树形dp)
题目链接:https://vjudge.net/problem/HDU-3586 题意:一棵边权树,要删掉一些边使得每个叶子结点不能到达树根,且这些边的权值<=上限Max,且边权和小于m,求最小 ...
- axios 使用post方式传递参数,后端接收不到
最近做vue项目,做图片上传的功能,使用get给后台发送数据,后台能收到,使用post给后台发送图片信息的时候,vue axios post请求发送图片base64编码给后台报错HTTP 错误 414 ...
- 小菜鸟之shell
Linux shell编程 目录 什么是Shell 1 Shell脚本的执行方式 1 第一种:输入脚本的绝对路径或相对路径 1 第二种:bash或sh +脚本 1 Shell中的变量 2 定义变量 2 ...
- Hadoop学习(2)-java客户端操作hdfs及secondarynode作用
首先要在windows下解压一个windows版本的hadoop 然后在配置他的环境变量,同时要把hadoop的share目录下的hadoop下的相关jar包拷贝到esclipe 然后Build Pa ...