Jireh程序猿的那些事 Jireh程序猿的那些事

记录分享生活、程序、信息的精彩人生

目录
Asp.Net WebForm实现ashx接口访问记录
/      

Asp.Net WebForm实现ashx接口访问记录

基于 IHttpModule 接口来实现

using Common;
using ControlClass;
using DataClass;
using Newtonsoft.Json;
using System;
using System.Web;

/// <summary>
/// LogAshxModule 的摘要说明
/// </summary>
public class LogAshxModule : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        HttpContext.Current.ApplicationInstance.PreRequestHandlerExecute += new EventHandler(this.log);
    }

    public void log(object sender, EventArgs e)
    {
        string url = ((System.Web.HttpApplication)(sender)).Request.RawUrl;
        if (url.Contains("ashx"))
        {
            var logBLL = new cs_request_log();
            var entity = new request_log();
            entity.AddTime = DateTime.Now;
            entity.Path = ((System.Web.HttpApplication)(sender)).Request.Path;
            entity.RawUrl = url;
            entity.QueryString = JsonConvert.SerializeObject(NVCExtender.ToDictionary(((System.Web.HttpApplication)(sender)).Request.QueryString));
            entity.Form = JsonConvert.SerializeObject(NVCExtender.ToDictionary(((System.Web.HttpApplication)(sender)).Request.Form));
            entity.BrowserInfo = ((System.Web.HttpApplication)(sender)).Request.UserAgent;
            entity.ClientIpAddress = ((System.Web.HttpApplication)(sender)).Request.UserHostAddress;
            entity.ClientName = ((System.Web.HttpApplication)(sender)).Request.UserHostName;

            var userModel = Ctrl_Users.GetModel();
            if (userModel != null)
            {
                entity.Uid = userModel.id;
            }
            logBLL.Add(entity);
        }   
        
    }
}

Web.config

还需要再web.config中配置modules

<system.webServer>
    <modules>
      <add name="LogAshxModule" type="LogAshxModule" />
    </modules>
  </system.webServer>

如果觉得这篇文章不错的话,请我喝一杯 咖啡☕吧
标题:Asp.Net WebForm实现ashx接口访问记录
作者:Jireh
地址:https://jireh.xyz/articles/2021/04/01/1617263953910.html
本作品由 Jireh 采用 署名 – 非商业性使用 – 禁止演绎 4.0 国际许可协议进行许可,转载请注明出处。