如何处理 CSS 本地与生产中不断变化的 URL

发布于 2024-12-11 19:48:02 字数 385 浏览 0 评论 0 原文

我们使用 Visual Studio 2010,在生产环境中我们将大量资源放在 CDN 上。然而,当我们在本地调试时,我们希望从相对本地路径中拉取资源。

因此,在我们的 CSS 中,我希望能够像在本地运行 CSS 一样使用 CSS:

.foo {
    background:url(../images/bar.jpg);
}

但是,当我发布到 Azure 时,我希望 CSS 自动更新路径为:

.foo {
    background:url(http://cdn.company.com/website/images/bar.jpg);
}

这在 Visual Studio 2010 中可能吗?

We are using Visual Studio 2010 and in our production environment we put a bunch of our resources on a CDN. However, when we are debugging locally, we want to pull the resources from a relative local path.

So in our CSS, I'd like to be able to have our CSS as when I run it locally:

.foo {
    background:url(../images/bar.jpg);
}

However, when I publish to Azure, I want to have the CSS automatically update the path to:

.foo {
    background:url(http://cdn.company.com/website/images/bar.jpg);
}

Is this possible in Visual Studio 2010?

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

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

发布评论

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

评论(3

我很坚强 2024-12-18 19:48:02

我意识到这可能不是您正在寻找的答案,但作为参考,您可以使用 MSBuild 社区任务,如下所示:

<PropertyGroup>
    <Configuration>Debug</Configuration>
    <TargetAddress>../images</TargetAddress>
    <TargetAddress Condition="'$(Configuration)'=='Release'">http://cdn.company.com/website/images</TargetAddress>
</PropertyGroup>

<Target Name="DeployCSS">
    <ItemGroup>
      <ScriptFiles Include=".\css\*.css"></ScriptFiles>
    </ItemGroup>
    <Copy SourceFiles="@(ScriptFiles)" DestinationFiles="@(ScriptFiles->'$(DeployPath)css\%(Filename)%(Extension)')"></Copy>
    <FileUpdate Encoding="ASCII" Files="@(ScriptFiles->'$(DeployPath)css\%(Filename)%(Extension)')" Regex="\.\./images" ReplacementText ="$(TargetAddress)"/>
</Target>

I realize this likely isn't the answer you're looking for, but for reference, you could do this with the FileUpdate Task in MSBuild Community Tasks, which would look something like this:

<PropertyGroup>
    <Configuration>Debug</Configuration>
    <TargetAddress>../images</TargetAddress>
    <TargetAddress Condition="'$(Configuration)'=='Release'">http://cdn.company.com/website/images</TargetAddress>
</PropertyGroup>

<Target Name="DeployCSS">
    <ItemGroup>
      <ScriptFiles Include=".\css\*.css"></ScriptFiles>
    </ItemGroup>
    <Copy SourceFiles="@(ScriptFiles)" DestinationFiles="@(ScriptFiles->'$(DeployPath)css\%(Filename)%(Extension)')"></Copy>
    <FileUpdate Encoding="ASCII" Files="@(ScriptFiles->'$(DeployPath)css\%(Filename)%(Extension)')" Regex="\.\./images" ReplacementText ="$(TargetAddress)"/>
</Target>
淡淡の花香 2024-12-18 19:48:02

先生,你来了。与大多数开发人员一样,我建议为您需要处理的各种应用程序使用单独的样式表。 ;)

您只需在 中的标记中添加一个 Literal 即可。像这样...

<asp:Literal ID="DynamicCSS" runat="server"></asp:Literal>

然后使用以下代码根据请求来源动态加载它们。

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    If Request.Url.Host = "localhost" Then
        DynamicCSS.Text = "<link type=""text/css"" rel=""Stylesheet"" href=""Styles/style1.css"" />"
    Else
        DynamicCSS.Text = "<link type=""text/css"" rel=""Stylesheet"" href=""Styles/style2.css"" />"
    End If
End Sub
private void Page_Init(object sender, System.EventArgs e) {
    if (Request.Url.Host == "localhost") {
        DynamicCSS.Text = "<link type=\"text/css\" rel=\"Stylesheet\" href=\"Styles/style1.css\" />";
    } else {
        DynamicCSS.Text = "<link type=\"text/css\" rel=\"Stylesheet\" href=\"Styles/style2.css\" />";
    }
}

注意:我没有测试 C# 代码,但我想它的工作原理应该是一样的。干杯;)

Here you are sir. As do most developers, I recommend using separate stylesheets for the various applications you need to address. ;)

You can simply add a Literal to your markup in your <head> like so...

<asp:Literal ID="DynamicCSS" runat="server"></asp:Literal>

Then use the following code to load them dynamically based on the request origin.

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    If Request.Url.Host = "localhost" Then
        DynamicCSS.Text = "<link type=""text/css"" rel=""Stylesheet"" href=""Styles/style1.css"" />"
    Else
        DynamicCSS.Text = "<link type=""text/css"" rel=""Stylesheet"" href=""Styles/style2.css"" />"
    End If
End Sub
private void Page_Init(object sender, System.EventArgs e) {
    if (Request.Url.Host == "localhost") {
        DynamicCSS.Text = "<link type=\"text/css\" rel=\"Stylesheet\" href=\"Styles/style1.css\" />";
    } else {
        DynamicCSS.Text = "<link type=\"text/css\" rel=\"Stylesheet\" href=\"Styles/style2.css\" />";
    }
}

Note: I didn't test the C# code, but I guess it ought to work the same. Cheers ;)

醉生梦死 2024-12-18 19:48:02

您可以将基本 url 放置在 web.config 文件中,并使用 Web 配置转换根据其发布位置对其进行修改。

一些文档:
http://msdn.microsoft.com/en-us/library/dd465326.aspx

You could place the base url in the web.config file, and use web config transforms to modify it based on where it is published.

Some Documentation:
http://msdn.microsoft.com/en-us/library/dd465326.aspx

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