Asp.net HttpModule中断WebResource.axd
我编写了一个HttpModule,它可以进行一些简单的内容重写。它工作正常,但是它有一个令人讨厌的副作用,WebResource.axd的输出是零字节。我在网上看到了一些关于这个已知问题的参考资料,但没有像我这样的过滤器 我是否在这里做了(或忽略了)会导致WebResource.axd崩溃的事情?我正在IIS 7.5上使用ASP.NET 4.0Asp.net HttpModule中断WebResource.axd,asp.net,vb.net,httpmodule,webresource.axd,Asp.net,Vb.net,Httpmodule,Webresource.axd,我编写了一个HttpModule,它可以进行一些简单的内容重写。它工作正常,但是它有一个令人讨厌的副作用,WebResource.axd的输出是零字节。我在网上看到了一些关于这个已知问题的参考资料,但没有像我这样的过滤器 我是否在这里做了(或忽略了)会导致WebResource.axd崩溃的事情?我正在IIS 7.5上使用ASP.NET 4.0 Imports System.Text Imports System.Text.RegularExpressions Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Web
Namespace HttpModules
Public Class ResponseRewritingModule
Implements IHttpModule
Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init
AddHandler app.ReleaseRequestState, AddressOf Me.OnReleaseRequestState
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
Public Sub OnReleaseRequestState(ByVal s As Object, ByVal e As EventArgs)
Dim app As HttpApplication = CType(s, HttpApplication)
If (app.Request.Url.ToString().Contains(".aspx")) Then
app.Response.Filter = New ImageUrlResponseFilter(app.Response.Filter)
End If
End Sub
End Class
Public Class ImageUrlResponseFilter
Inherits MemoryStream
Private responseStream As Stream
Private matchpattern As String = "src=""/Images/"
Private matchpattern2 As String = "url\(/Images"
Private matchpattern3 As String = "src = ""/Images/"
Private matchpattern4 As String = "src=""http://www.mydomain.com/Images/"
Private replacementstring As String = "src=""http://images.mydomain.com/Images/"
Private replacementstring2 As String = "url(http://images.mydomain.com/Images"
Public Sub New(inputStream As Stream)
responseStream = inputStream
End Sub
Public Overrides Sub Write(buffer As Byte(), offset As Integer, count As Integer)
Dim strBuffer As String = System.Text.UTF8Encoding.UTF8.GetString(buffer)
If (Not HttpContext.Current.Request.IsSecureConnection) Then
strBuffer = Regex.Replace(strBuffer, matchpattern, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
strBuffer = Regex.Replace(strBuffer, matchpattern2, replacementstring2, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
strBuffer = Regex.Replace(strBuffer, matchpattern3, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
strBuffer = Regex.Replace(strBuffer, matchpattern4, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
responseStream.Write(UTF8Encoding.UTF8.GetBytes(strBuffer), offset, UTF8Encoding.UTF8.GetByteCount(strBuffer))
Else
' Do nothing for SSL connections
responseStream.Write(buffer, offset, count)
End If
End Sub
End Class
End Namespace
更新:丹尼尔·理查森似乎已经发现了根本原因,并在这里找到了解决方案:
微软显然已经知道这个问题很多年了,并且不会解决它:StringBuilder会给我带来什么好处?我的问题是为什么启用此模块会破坏WebResource.axd。你所说的图像过滤器是什么意思?ControlAdapter假设我使用的是我所有的图像,不是吗?情况绝对不是这样。我已经看到许多基准测试证明StringBuilder在所有情况下都会更快。无论如何,我的问题根本不是关于性能,而是为什么WebResource.axd会崩溃。如果WebResource像您建议的那样脆弱,那么没有人会使用HttpModules,这几乎是不正确的。到Daniel Rochardson的博客的链接现在是无效的。是否有人知道一个概述(或原始文章的链接)可以为这个问题的解决方案提供一些(具体的)线索?看起来丹尼尔·理查森的博客文章的细节可以在相关问题的解决方法中找到: