How to inject HTML before closing html tag in a HTTPModule

二次信任 提交于 2019-12-09 23:57:45

问题


I'm trying to inject some additional markup in the response but its not outputting as expected.

For example the following code will output:

... </html>CONTENT

private void OnEndRequest(Object source, EventArgs e)
{
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;
    context.Response.Write("CONTENT");
}

But i want the content to output right before the closing html tag

... CONTENT</html>

Any ideas on how to achieve this?


回答1:


You should use an HttpContext.Response.Filter.

Check this MSDN documentation page:

  • http://msdn.microsoft.com/en-us/library/system.web.httpresponse.filter.aspx

This very old article should clarify this too:

  • http://ondotnet.com/pub/a/dotnet/2003/10/20/httpfilter.html

Summarizing, you need to create a Stream implementation wrapping the original one coming in HttpContext.Current.Response.Filter.

In order to inject HTML in some part of the document, you simply need to convert bytes into a string and using a String.IndexOf or a regular expression you're going to deremine if you're in the whole code line.

After that, just concatenate, modify or replace obtained string and put it in the wrapped Stream coming in the original, default response filter.



来源:https://stackoverflow.com/questions/8678424/how-to-inject-html-before-closing-html-tag-in-a-httpmodule

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