Tag: httpmodule

在HttpModule中挂钩到Application_Start

我正在实现一个简单的HttpModule,我想在Web应用程序启动时运行一些代码。 但我很惊讶地发现我通常从Global.asax使用的Application_Start事件不能从HttpModule获得。 这是正确的,还是我在这里遗漏了什么? 如何从HttpModule挂钩到Application_Start事件? 更新: 我使用Init事件来简单解决这个问题,但它对我来说仍然有点滑稽。

是否可以在IIS HttpModule中修改HttpRequest POST的内容?

我需要在IIS中修改某些HttpRequests(SSAS连接字符串)的内容。 基本上,我需要向请求中包含的SOAP添加一个元素。 到目前为止,我的方法是向HttpRequest添加一个Filter,并在filter的Read方法中执行更改。 据我所知,Read永远不会被执行。 我对Request.Filter的理解是它从IIS处理请求时读取,因此IIS应该看到我修改过的Request。 我正在尝试使用HttpModule实际可行,并且我的Filter方法是否正确? 如果是这样,什么会导致Read不被击中? 这是我的代码的简化版本: public class CustomHttpModule : IHttpModule { private HttpApplication app; public string ModuleName { get { return “CustomHttpModule”; } } public void Init(HttpApplication context) { app = context; context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute); } void context_PreRequestHandlerExecute(object sender, EventArgs e) { var request = app.Context.Request; request.Filter = new CustomHttpFilter(request.Filter); } […]

IIS在第一个请求中对URL中的双重编码正斜杠的处理方式与后续请求的处理方式不同

最近,我的团队被要求为ASP.NET MVC应用程序实现HttpModule,该应用程序处理IIS 7和.NET 3.5上的双重编码URL。 这是问题的症结所在: 我们有时会获得具有双重编码正斜杠的URL,如下所示: http://www.example.com/%252fbar%5cbaz/foo 还有其他格式我们必须处理,但它们都有一些共同点,它们有一个双重编码的正斜杠。 为了解决这个问题,我们编写了一个HttpModule,它仅在URL具有双重编码的正斜杠时起作用,并且我们将其重定向到一个合理的URL。 细节并不重要,但有两个位: 我们无法控制这些URL具有双重编码正斜杠的事实 我们还没有对.NET 4.0进行过分析,也没有立即开始。 这是问题所在: IIS启动后的第一个请求显示的URL与第二个请求不同。 如果我们使用上面示例中的URL,则对IIS的第一个请求如下所示: http://www.example.com/bar/baz/foo 第二个请求看起来像: http://www.example.com/%252fbar%5cbaz/foo 这是通过在调试时检查Application.Request.Url.AbsolutePath属性来完成的。 这是应该重现问题的最小代码示例(创建一个新的MVC应用程序,并注册以下HttpModule): public class ForwardSlashHttpModule : IHttpModule { internal IHttpApplication Application { get; set; } public void Dispose() { Application = null; } public void Init(HttpApplication context) { Initialize(new HttpApplicationAdapter(context)); } internal void Initialize(IHttpApplication context) { […]

如何使用自定义IHttpModule和HttpRequestfilter修改POST请求?

概观 我希望能够将请求参数和内容修改为第三方Web服务(ArcGIS Server)。 这将用于创建存在于任何客户端应用程序和服务器应用程序之间的安全层。 我认为我找到了一个解决方案,但我目前在实施方面遇到了一些困难。 可能的解决方案:使用自定义请求筛选器修改请求 对于解决方案,我基于MSDN上显示的示例松散地实现了自定义请求filter。 我已经“增强”了代码,以便我可以使用正则表达式搜索和替换必要的内容。 这包括: 将内容(存储在字节数组中)转换为字符串。 搜索字符串并执行任何必要的修改。 将修改后的字符串转换为字节数组并将其写入缓冲区。 一个例子如下所示: public override int Read(byte[] buffer, int offset, int count) { int bytesRead = _stream.Read(buffer, offset, count); string orgContent = Encoding.UTF8.GetString(buffer, offset, bytesRead); string orgContentDecoded = HttpUtility.UrlDecode(orgContent); string layersPattern = @”&layers=(show|hide|include|exclude):([0-9]+,?)+”; Regex layersRegex = new Regex(layersPattern, RegexOptions.IgnoreCase); string[] permittedLayers = new string[] { […]

httpModules不能在iis7上运行

我有以下模块 public class LowerCaseRequest : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(this.OnBeginRequest); } public void Dispose() { } public void OnBeginRequest(Object s, EventArgs e) { HttpApplication app = (HttpApplication)s; if (app.Context.Request.Url.ToString().ToLower().EndsWith(“.aspx”)) { if (app.Context.Request.Url.ToString() != app.Context.Request.Url.ToString().ToLower()) { HttpResponse response = app.Context.Response; response.StatusCode = (int)HttpStatusCode.MovedPermanently; response.Status = “301 Moved Permanently”; response.RedirectLocation = […]

如何分析ASP.NET MVC应用程序中的请求性能?

我想捕获ASP.NET MVC应用程序中的请求的命中时间,处理时间,内存消耗和响应时间。 有没有办法或工具来执行此操作?