How can I programmatically modify an aspx file content before the handler handle it?

无人久伴 提交于 2019-12-13 03:56:24

问题


The reason I need to modify the content of an aspx (not physically, but change the content in the memory) is because there are certain custom tags I made needs to be parsed to the correct data before the entire aspx is handled by the HttpHandler.

Is there a way for that?


回答1:


You can use Response Filters (HttpFilter) and modify content on the fly, basically after response is formed, before EndRequest your filter is called (it's a stream descendant) and you can modify it as you wish. In the HttpModule, Init method you got to install HttpFilter (Response.Filter) and it will be called for that request.

Here is a good article :

http://aspnetresources.com/articles/HttpFilters

UPDATE: Maybe this is a case of XY Problem, and you can solve your problem with simple server control that will render these custom tags properly.




回答2:


you can use the Render event

Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)


    Dim sw As New System.IO.StringWriter
    Dim hw As New HtmlTextWriter(sw)
    MyBase.Render(hw)
    Dim html As String = sw.ToString()

    ' html = html.Replace() etc to change your html code in here

    writer.Write(html)
End Sub

EDIT i see you want to inject mark up dynamically before asp.net handles the aspx, maybe the FileLevelPageControlBuilder Class is of use



来源:https://stackoverflow.com/questions/10215284/how-can-i-programmatically-modify-an-aspx-file-content-before-the-handler-handle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!