I have a page that does n开发者_运维百科ot have runat="server" set in the <head/>
section. I do not have access to modify any of the code in the page.
This page contains a user control which I do have access to. Can I add a <meta/>
tag to the head section of the page from the user control? It needs to be server-side so a javascript solution won't work.
One option is to create a Response Filter, and then modify the output before it's sent to the user.
https://web.archive.org/web/20211029043851/https://www.4guysfromrolla.com/articles/120308-1.aspx
You can parse the text in
(this.Page.Controls[0] as LiteralControl).Text
to see where the string <head>
starts, and insert whatever text you need in there thus injecting your own code into the page header without it being marked with runat="server"
.
Please be aware though, this is pretty hacky way of getting your code where it most likely shouldn't be (otherwise the <head>
element would have been marked as runat="server"
so you can access it normally). This will also break if at a later date the head element is changed to be an ASP.NET control. It might will not work with master pages, you will have to walk up the control tree looking for topmost literal element.
精彩评论