博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET MVC 3–Global Action Filters
阅读量:5033 次
发布时间:2019-06-12

本文共 3371 字,大约阅读时间需要 11 分钟。

public class MvcProfilerGlobalAttribute : ActionFilterAttribute{//...}protected void Application_Start(){    //...    GlobalFilters.Filters.Add(new MvcProfilerGlobalAttribute());}public class MvcProfilerGlobalAttribute : ActionFilterAttribute  {      public override void OnActionExecuting(ActionExecutingContext filterContext)      {          var httpContext = filterContext.RequestContext.HttpContext;          var httpRequest = httpContext.Request;          var inputStream = httpRequest.InputStream;          var requestBody = new StreamReader(inputStream).ReadToEnd();          inputStream.Seek(0, SeekOrigin.Begin);          }public class ProfileData   {       public string Url { get; set; }       public string RequestData { get; set; }       public string ResponseHttpStatusCode { get; set; }       public string RequestHeaders { get; set; }       public string ResponseHeaders { get; set; }       public string QueryStringData { get; set; }       public string ClientIp { get; set; }       public DateTime StartTime { get; set; }       public DateTime EndTime { get; set; }   } public class ProfilerContext : IDisposable   {       private readonly IProfilerLogRepository repository;       private const string ProfilerItemKey = "DEVELOQPROFILER";       private readonly Guid profilerContextIdentifier;       private readonly DateTime started;       public ProfileData ProfileData { get; private set; }       public ProfilerContext() : this(new InMemoryProfilerLogRepository()) { }       public ProfilerContext(IProfilerLogRepository repository)       {           this.repository = repository;           profilerContextIdentifier = Guid.NewGuid();           started = DateTime.UtcNow;           ProfileData = new ProfileData();       }       public void Persist()       {           repository.Add(ProfileData);       }       public void Dispose()       {           Persist();       }       public static ProfilerContext Current       {           get           {               var context = HttpContext.Current;               if (context == null) return null;               InitCurrentProfiler();               return context.Items[ProfilerItemKey] as ProfilerContext;           }           private set           {               var context = HttpContext.Current;               if (context == null) return;               context.Items[ProfilerItemKey] = value;           }       }       private static void InitCurrentProfiler()       {           var context = HttpContext.Current;           if (context == null) return;           if (context.Items[ProfilerItemKey] as ProfilerContext != null) return;           Current = new ProfilerContext();       }   }public interface IProfilerLogRepository    {        void Add(ProfileData profileData);        List
GetAll(); }public class InMemoryProfilerLogRepository : IProfilerLogRepository { private static List
profileLogs; public InMemoryProfilerLogRepository() { if (profileLogs == null) profileLogs = new List
(); } public void Add(ProfileData profileData) { profileLogs.Add(profileData); } public List
GetAll() { return profileLogs; } }

 

转载于:https://www.cnblogs.com/yezhi/archive/2013/01/23/2874053.html

你可能感兴趣的文章
Python 拓展之推导式
查看>>
[Leetcode] DP-- 474. Ones and Zeroes
查看>>
80X86寄存器详解<转载>
查看>>
c# aop讲解
查看>>
iterable与iterator
查看>>
返回顶部(动画)
查看>>
webpack+react+antd 单页面应用实例
查看>>
Confluence 6 SQL Server 数据库驱动修改
查看>>
Confluence 6 通过 SSL 或 HTTPS 运行 - 备注和问题解决
查看>>
【47.76%】【Round #380B】Spotlights
查看>>
Git(使用码云)
查看>>
分享Java web 开发必游之路
查看>>
IIS初始化(预加载),解决第一次访问慢,程序池被回收问题(转载)
查看>>
Bean的Scope
查看>>
【BZOJ】3142: [Hnoi2013]数列
查看>>
http初探
查看>>
elasticsearch的安装
查看>>
__next__()
查看>>
爬取:中国大学排名
查看>>
聊天室(C++客户端+Pyhton服务器)_1.框架搭设
查看>>