ImageResizer文档中的扩展点页面提到插件可以“修改HTTP标头”,但是它们没有任何方向或示例。我创建了一个同时实现IPlugin
和IChache
接口的插件来添加HTTP标头,但感觉不正确,原因是:
current.RemapHandler(New NoCacheHandler(e))
方法的末尾添加代码Process()
,它将无法正常工作(该图像不会显示/显示为空白图像)。我不知道这行在做什么,或者不知道重新映射处理程序的含义这是我当前的实现方式
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<resizer>
<plugins>
<add name="MyNamespace.CorsHeadersPlugin" />
<add name="Watermark" />
<add name="MvcRoutingShim" />
<add name="PsdReader" />
<add name="WicDecoder" />
<add name="WicEncoder" />
<add name="WicBuilder" />
</plugins>
</resizer>
...
</configuration>
CorsHeadersPlugin
Imports ImageResizer.Caching
Imports ImageResizer.Configuration
Imports ImageResizer.Plugins
Imports ImageResizer.Plugins.Basic
Public Class CorsHeadersPlugin
Implements IPlugin
Implements ICache
Private ReadOnly AllowedDomains As String()
Public Sub New()
AllowedDomains = {"http://allowthisdomain.com"}
#If DEBUG Then
AllowedDomains = AllowedDomains.Union({"http://localhost", "https://localhost"}).ToArray
#End If
End Sub
Public Function CanProcess(current As HttpContext, e As IResponseArgs) As Boolean Implements ICache.CanProcess
Return True
End Function
Public Sub Process(current As HttpContext, e As IResponseArgs) Implements ICache.Process
Dim origin = current.Request.Url.GetLeftPart(UriPartial.Authority)
If AllowedDomains.Contains(origin) Then
current.Response.Headers.Add("Access-Control-Allow-Origin", origin)
End If
current.RemapHandler(New NoCacheHandler(e))
End Sub
Public Function Install(c As Config) As IPlugin Implements IPlugin.Install
c.Plugins.add_plugin(Me)
Return Me
End Function
Public Function Uninstall(c As Config) As Boolean Implements IPlugin.Uninstall
c.Plugins.remove_plugin(Me)
Return True
End Function
End Class
答案 0 :(得分:1)
事件处理程序ImageResizer.Config.Current.Pipeline.PreHandleImage and .PreHandleImageAsync
允许您修改响应头。确保在AppStart期间仅注册一次处理程序。
答案 1 :(得分:0)
更新,有效的实施方式
Imports ImageResizer.Caching
Imports ImageResizer.Configuration
Imports ImageResizer.Plugins
Public Class CorsHeadersPlugin
Implements IPlugin
Private ReadOnly AllowedDomains As String()
Public Sub New()
AllowedDomains = {"http://allowthisdomain.com"}
#If DEBUG Then
AllowedDomains = AllowedDomains.Union({"http://localhost:3416", "https://localhost:3416"}).ToArray
#End If
End Sub
Public Function Install(c As Config) As IPlugin Implements IPlugin.Install
AddHandler c.Pipeline.PreHandleImage, AddressOf ModifyHttpHeaders
c.Plugins.add_plugin(Me)
Return Me
End Function
Public Function Uninstall(c As Config) As Boolean Implements IPlugin.Uninstall
RemoveHandler c.Pipeline.PreHandleImage, AddressOf ModifyHttpHeaders
c.Plugins.remove_plugin(Me)
Return True
End Function
Private Sub ModifyHttpHeaders(sender As IHttpModule, context As HttpContext, e As IResponseArgs)
Dim origin = GetRequestOrigin(context.Request)
If origin IsNot Nothing AndAlso AllowedDomains.Contains(origin) Then
context.Response.Headers.Add("Access-Control-Allow-Origin", origin)
End If
End Sub
Private Function GetRequestOrigin(request As HttpRequest) As String
If request.Headers("Origin") IsNot Nothing Then
Return request.Headers("Origin")
ElseIf request.UrlReferrer IsNot Nothing Then
Return request.UrlReferrer.GetLeftPart(UriPartial.Authority)
End If
Return Nothing
End Function
End Class