ASP.NET:动态更改静态资源的链接

发布于 2024-07-26 06:49:48 字数 380 浏览 2 评论 0原文

我们正在构建一个内部静态资产服务器。 每个环境(dev、staging、prod)都有自己的 asset 服务器,并且 asset 在整个 Web 应用程序中被引用(html、aspx、ascx、css、javascript 等...)

以在正确的环境中引用正确的 asset 服务器,一种解决方案是编写一个 http 模块,在响应到达客户端之前拦截响应并据此更改 URL。 我只是认为这可能不是最具可扩展性的解决方案,因为这个 http 模块将针对每个请求执行,并在客户端获取之前基本上解析整个响应(有些响应很大)。

我也在考虑使用客户端 javascript 来更改客户端的引用,但这可能不像 http 模块那样有效。

有什么想法吗? ASP.NET 的行业最佳实践是什么?

We are building a internal static asset server. Each environment (dev, staging, prod) has its own asset server, and the asset is reference throughout the web application (html, aspx, ascx, css, javascript, etc...)

To reference the correct asset server in the correct environment, one solution is to write a http module to intercept response before it gets to the client and change the URL according. I am just thinking that this might not be the most scalable solution since this http module is going to get executed for every request and basically parse the whole response (some are huge) before the client gets it.

I am also thinking to use a client side javascript to change the reference on client side, but this might not work as nicely has a http module.

Any thoughts? What's the industries best practice in ASP.NET?

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

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

发布评论

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

评论(2

寄意 2024-08-02 06:49:48

我可能会创建自己的 LinkToStaticAsset 控件。 它只接受相对于每个资产的静态资产服务器的路径。 我希望它通过包含配置中的基本路径来生成完整的 URL。

I might create my own LinkToStaticAsset control. It would only accept the path relative to the static asset server of each asset. I would have it generate the full URL by including the base path from configuration.

暖心男生 2024-08-02 06:49:48

如果您希望您的网站在不依赖 Javascript 的情况下运行(即使只是部分运行),那么您应该将此保留在服务器端。

上面提到的另一种方法是在应用程序级别执行此操作,即使用一个库方法来生成静态资产 URL 并配置为通过 web.config 指向特定服务器。


它会像这样:-(

在您的 App_Code 文件夹或引用的程序集中)

public static class Util
{
    public static string AssetUrl(string relativePath)
    {
        // returns asset server address from web config with relative path appended
    }
}

(在 web.config 中)

<appSettings>
    <add key="AssetServerBaseUrl" value="http://foo.bar" />
    ...
</appSettings>

(在您的 aspx 文件中)

<img src='<%= Util.AssetUrl("img/myimage.jpg") %>' ... />

If you want your site to function (even if only partially) without a Javascript dependency then you should keep this server-side.

An alternative approach to what you have mentioned above is to do this at the application level, i.e. have a library method which generates your static asset URL's and is configured to point at particular server(s) via your web.config.


It would go something like this:-

(In your App_Code folder or a referenced assembly)

public static class Util
{
    public static string AssetUrl(string relativePath)
    {
        // returns asset server address from web config with relative path appended
    }
}

(In web.config)

<appSettings>
    <add key="AssetServerBaseUrl" value="http://foo.bar" />
    ...
</appSettings>

(In your aspx file)

<img src='<%= Util.AssetUrl("img/myimage.jpg") %>' ... />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文