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.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
更新: Daniel Richardson 似乎已经发现了根本原因,并在这里提供了解决方案:http://daniel-richardson.blogspot.com/2008/11/how-to-apply-filter-to-content-returned.html
微软显然已经知道这个问题很多年了并且不会修复它:http://connect.microsoft.com/VisualStudio/feedback/details/105150/webresource-axd-output-always-truncated-when-used-with-a-response-filter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论