问题
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