ASP.NET IHttpModule IHttpHandler IHttpHandlerFactory 拦截请求

先来看看代码,拦截所有Http请求类。下面包含了两种类的集成 IHttpModule IHttpHandlerFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SoonnetWebSite
{
    /// 
    /// Handler2 的摘要说明
    /// 
    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
{
    /// 
    /// Handler1 的摘要说明
    /// 
    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;
            }
        }
    }
}

两端代码的配置如下

  
    
      
        
      
    
    
    
      
      
      
    
    
      
    
  

  发现个问题, IIS 配置的跟本机测试的节点不一样。线上的配置要用


    
      
        
      
    
    
    
      
      
      
    
    
      
    
  

 前面配置多了一个http

是因为IIS 的经典模式跟集成模式的关系,

参考链接:https://blog.csdn.net/hongwei_23/article/details/44300923

 

http 转 https 参考链接:https://blog.csdn.net/suxuelian/article/details/80103514

转载于:https://www.cnblogs.com/Mrly/p/11497671.html

你可能感兴趣的:(ASP.NET IHttpModule IHttpHandler IHttpHandlerFactory 拦截请求)