将Meta标签插入网页

时间:2013-04-19 15:30:11

标签: html internet-explorer iis-7

前段时间我设计了一个应用程序,它会在我的页面中插入元标记,以便我可以在不同的文档模式下使用Internet Explorer测试它们。我的解决方案,虽然功能齐全,但却很麻烦。假设IIS7和资源管理器作为服务器/客户端,有哪些轻量级解决方案可以在短时间内以编程方式快速添加元标记。

1 个答案:

答案 0 :(得分:0)

您可以使用URL Rewrite轻松完成此操作并利用Outbound Rewrite功能。安装完成后,只需将web.config添加到应用程序文件夹,如下所示,它将自动将META标记插入到应用程序提供的每个HTML页面。显然,您可以添加更多条件,并仅对您想要的页面进行重写(请参阅前置条件),以及从标题或其他位置捕获数据并将其添加到响应中。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <outboundRules rewriteBeforeCache="true">
                <rule name="WriteMETA" preCondition="MatchHTML">
                    <match pattern="&lt;head&gt;" occurrences="1" />
                    <action type="Rewrite" value="&lt;head>&#10;
                        &lt;meta name='author' content='Carlos Aguilar Mares' />
                        " />
                </rule>
                <preConditions>
                    <preCondition name="MatchHTML" patternSyntax="Wildcard">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>