Can I make IIS add (inject) HTML to every page it serves?

后端 未结 4 1184
清歌不尽
清歌不尽 2021-01-14 11:21

I would like to add some HTML to every page that our IIS 6 server serves. It\'s serving static HTML for the most part. Is this something IIS or an extension can do? I wou

相关标签:
4条回答
  • 2021-01-14 11:38

    I was able to inject some CSS before the using the URL Rewrite Module, via an outbound rule:

    <rewrite>
        <rules>
            <outboundRules rewriteBeforeCache="true">
                <rule name="Add custom CSS" preCondition="IsHTML">
                    <match filterByTags="None" pattern="&lt;/head>" />
                    <action type="Rewrite" value="&lt;link rel=&quot;stylesheet&quot; href=&quot;/path/to/custom/styles_override.css&quot;>&lt;/head>" />
                </rule>
                <preConditions>
                    <preCondition name="IsHTML">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rules>
    </rewrite>
    

    You should be able to do the same with html content, instead of css...

    0 讨论(0)
  • 2021-01-14 11:47

    If you're familiar with ASP.NET, you could write a HTTP Response Filter to do that.

    Read this article by Milan Negovan.

    The HttpResponse class has a very useful property:

    public Stream Filter {get; set;}
    

    MSDN provides a helpful description of this property: "Gets or sets a wrapping filter object used to modify the HTTP entity body before transmission." Confused? In other words, you can assign your own custom filter to each page response. HttpResponse will send all content through your filter. This filter will be invoked right before the response goes back to the user and you will have a change to transform it if need be.

    This could be extremely helpful if you need to transform output from "legacy" code or substitute placeholders (header, footer, navigation, you name it) with proper code. Besides, at times it's simply impossible to ensure that every server control plays by the rules and produces what you expect it to. Enter response filters.

    The Filter property is of type System.IO.Stream. To create your own filter you need to derive a class from System.IO.Stream (which is an abstract class) and add implementation to its numerous methods.

    0 讨论(0)
  • 2021-01-14 11:51

    In IIS proper, you can add a footer, which is great for a copyright line, or similar. If you want more control, to truly "inject", I would create an HTTP Handler (.NET) that handles .html requests and adds what you need.

    If you are "old school", use ISAPI filters instead. Too much work for my tastes.

    0 讨论(0)
  • 2021-01-14 11:54

    Natively I believe the only thing you can do is insert a document footer (on the Documents tab).

    0 讨论(0)
提交回复
热议问题