使用ASP.NET动态设置CSS值 [英] Dynamically setting CSS values using ASP.NET

查看:162
本文介绍了使用ASP.NET动态设置CSS值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上工作,图片和其他资源将位于与网站主要内容不同的域中。我们将使用类似www.example.com的主网站,然后使用images.example.com为所有额外的资源样式等。

I'm working on a site where the images and other resources will be located on a separate domain from the main content of the site. We will use something like 'www.example.com' for the main site, and then 'images.example.com' for all extra resources for styles, etc.

当开发网站时,我会将所有这些资源保存在本地dev。机器。这里的挑战是保持生产服务器和开发环境之间的CSS引用一致。

When developing the site I will keep all of these resources on local dev. machines. The challenge here is keeping CSS references consistent between the production server and development environments.

我想的是创建一个 web.config 键,将存储图像服务器的URL。然后,当从开发切换到生产时,我只需要改变web.config的值,一切就可以了。

What I was thinking of doing was creating a web.config key that would store the URL of the images server. Then, when switching from development to production I could just change the web.config value and everything would be done.

有什么方法可以给CSS文件添加一个值,动态或其他方式,从某个地方在一个配置或C#类?

Is there any way to add a value to a CSS file, dynamically or otherwise, from some place in a config or C# class? Or am I going about this the wrong way?

此外,我限制使用.NET 2.0,如果这有什么区别。

Also, I'm limited to using .NET 2.0 if that makes a difference.

UPDATE

要进一步了解这一点,我知道我可以使用web.config设置为服务器控件的URL。这些已经动态生成。我更感兴趣的是我有修改(或做某事)到静态CSS文件,这将允许我更改URL的背景图片资源,将被引用的选项在CSS。有什么我可以做,除了使用我的IDE查找/替换值?

UPDATE
To expand on this a little more, I know I can use a web.config setting for server controls' URLs. Those are already generated dynamically. What I'm more interested in is what options I have for modifying (or doing "something") to static CSS files that will allow me to change URLs for things such as background image resources that would be referenced in CSS. Is there anything I can do besides find/replacing the values using my IDE? Perhaps something that can be done automatically with a deployment script?

推荐答案

是否在图像服务器上保留CSS文件是一个选项?如果可能,你可以使所有的图像引用相对,然后你只需要更新css文件的链接。

Is keeping the CSS file on the image server an option? If that it possible, you could make all the image references relative, and then you just need to update the link to the css file.

<link rel="stylesheet" href="<%= ConfigurationManager.AppSettings("css-server") %>style.css" />

如果您仍然希望动态发送或生成css文件:

If you still want to send or generate a css file dynamically:

css文件不必以css结尾。 aspx是罚款。您可以这样做:

css files don't have to end in css. aspx is fine. You could do this:

<link rel="stylesheet" href="style.aspx" />

,然后在style.aspx页面中:

and then in your style.aspx page:

protected void page_load(){
    Response.ContentType = "text/css";
    if (ConfigurationManager.AppSettings("css-server") == "local") {
        Server.Transfer("css/local.css");
    } else {
        Server.Transfer("css/production.css");
    }   
}

如果您仍然希望动态生成css文件,我将使用HttpHandler,将contenttype设置为text / css,然后使用Response.Write生成css。如果你坚持让页面结束在css,你总是可以注册css到asp.net在IIS中,然后在global.asax application_Begin请求中的传入请求,如果文件以.css结尾,请使用httpcontext.current.rewritepath

If you still want to dynamically generate a css file, I'd use an HttpHandler, set the contenttype to "text/css", then generate the css with Response.Write. If you insist on having the page end in css, you could always register css to go to asp.net in IIS, then on incoming requests in global.asax application_Begin request, if the file ends in .css, use httpcontext.current.rewritepath to your handler.

这将会在运行时动态生成style.css的净效果。

This will have a net effect of style.css being dynamically generated at runtime.

这篇关于使用ASP.NET动态设置CSS值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆