将url参数添加到asp主题文件夹中的css文件中

发布于 2024-08-16 22:55:04 字数 450 浏览 8 评论 0原文

我编写了一些有助于 js 文件版本控制的代码。本质上,它围绕当前脚本管理器旋转,并附加 javascript 文件路径和文件的 md5 哈希值。所以

<script src="../Javascript/Navigation.js" type="text/javascript"></script>

<script src="../Javascript/Navigation.js?md5=70D2B4D1F236C7E340D9152B9E4102C3" type="text/javascript"></script>

认为这是一件相当常见的事情(或其变体)。我正在努力做的是拿起 app_themes 文件夹中的 css 文件并执行相同的操作。

如何进入并更改 css 链接?

I have written a bit of code that helps with versioning of js files. Essentially it spins around the current script manager and appends the javascript file path with an md5 hash of the file. So

<script src="../Javascript/Navigation.js" type="text/javascript"></script>

becomes

<script src="../Javascript/Navigation.js?md5=70D2B4D1F236C7E340D9152B9E4102C3" type="text/javascript"></script>

I think this is a fairly common thing to do (or variants of it). What I am struggling to do is pick up the css files in the app_themes folder and do the same.

How do I get in and change the css links?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿挽旧人 2024-08-23 22:55:05

您可以使用控件适配器将此行为巧妙地注入到页面中,如下所示:

public class PageAdapter : System.Web.UI.Adapters.PageAdapter
{
    protected override void OnPreRender(System.EventArgs e)
    {
        foreach (var link in Page.Header.Controls.OfType<HtmlLink>().ToList())
            if (link.Attributes["type"].Equals("text/css", StringComparison.OrdinalIgnoreCase))
                if (link.Attributes["href"].Contains("/App_Themes/{0}/".Fill(Page.Theme), StringComparison.OrdinalIgnoreCase))
                   /* process link */

        base.OnPreRender(e);
    }
}

您可以通过将以下内容保存为 App_Browsers 文件夹中的 *.browser 文件来插入它:

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.Page"
               adapterType="PageAdapter" />
    </controlAdapters>
  </browser>
</browsers>

总的来说,我认为控件适配器是一种强大的类似AOP的机制,用于将行为注入到控件/页面生命周期中;它们几乎被完全忽略,而倾向于传统的子分类。

You can use a control adapter to neatly inject this behavior into the page as follows:

public class PageAdapter : System.Web.UI.Adapters.PageAdapter
{
    protected override void OnPreRender(System.EventArgs e)
    {
        foreach (var link in Page.Header.Controls.OfType<HtmlLink>().ToList())
            if (link.Attributes["type"].Equals("text/css", StringComparison.OrdinalIgnoreCase))
                if (link.Attributes["href"].Contains("/App_Themes/{0}/".Fill(Page.Theme), StringComparison.OrdinalIgnoreCase))
                   /* process link */

        base.OnPreRender(e);
    }
}

You can plug it in by saving the following as a *.browser file in the App_Browsers folder:

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.Page"
               adapterType="PageAdapter" />
    </controlAdapters>
  </browser>
</browsers>

Overall, I think Control Adapters are a powerful AOP-like mechanism for injecting behavior into control/page life-cycles; they are almost entirely ignored in favor of traditional sub-classing.

迷你仙 2024-08-23 22:55:05

我面临一个问题,即每次回发时都会在 html 标记中重复 css 条目。
例如,我有 newabc.css。该代码会将其更改为 newabc.css?v=1。如果我在 5 次回发后看到 html 源,它将有 5 个“newabc.css?v=1”。所以我添加了 link.EnableViewState = False,它工作正常,但实际上需要吗?

    Dim link As HtmlLink = Nothing

    For Each c As Control In Page.Header.Controls
        If TypeOf c Is HtmlLink Then
            link = TryCast(c, HtmlLink)

            If link.Href.IndexOf("App_Themes/", StringComparison.InvariantCultureIgnoreCase) >= 0 AndAlso link.Href.EndsWith(".css", StringComparison.InvariantCultureIgnoreCase) Then
                link.Href &= "?v=" & VER_CSS
                'link.EnableViewState = False
            End If
        End If
    Next

I am facing one issue that it is repeating css entry in html markup on every postback.
for example, I have newabc.css. the code will change it to newabc.css?v=1. if I see html source after 5 postback, it will have 5 "newabc.css?v=1". so I added link.EnableViewState = False, it works fine but is it actually needed?

    Dim link As HtmlLink = Nothing

    For Each c As Control In Page.Header.Controls
        If TypeOf c Is HtmlLink Then
            link = TryCast(c, HtmlLink)

            If link.Href.IndexOf("App_Themes/", StringComparison.InvariantCultureIgnoreCase) >= 0 AndAlso link.Href.EndsWith(".css", StringComparison.InvariantCultureIgnoreCase) Then
                link.Href &= "?v=" & VER_CSS
                'link.EnableViewState = False
            End If
        End If
    Next
a√萤火虫的光℡ 2024-08-23 22:55:05

使用 Combres 来避免重复发明轮子。满足您要求的一切,甚至更多!

Avoid to invent the wheel twice by using Combres instead. Does everything you ask for, and more!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文