c# - StyleBundle 打破相对路径
问题描述
我的问题与此类似:
总结:
在 css 中使用相对链接时,链接将在捆绑后断开。
例子:
原来的:
.flags {
background-image: url("../images/flags.png");
}
对服务器的请求:
GET https://{HOST}/myapp/images/flags.png 200
捆绑后:
.flags {background-image: url("../images/flags.png");}
对服务器的请求:
GET https://{HOST}/images/flags.png 404
我想要什么(捆绑后)
.flags {background-image: url("/myapp/images/flags.png");}
或者
.flags {background-image: url("images/flags.png");}
我的 BundleConfig.cs
using System.Web;
using System.Web.Optimization;
namespace MyApp
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
// CSS
bundles.Add(new StyleBundle("~/App/bundledCSS")
.Include("~/App/css/index.css", new CssRewriteUrlTransformWrapper())
);
[JS Bundels, ...]
}
}
public class CssRewriteUrlTransformWrapper : IItemTransform
{
public string Process(string includedVirtualPath, string input)
{
return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);
}
}
}
在我的 index.cshtml
@Styles.Render("~/App/bundledCSS")
我尝试了两者的解决方案,但没有得到有效的结果
我使用的其他资源:
此外,很高兴知道是否可以使用该解决方案IncludeDirectory
或使其StyleBundle
影响 a 中的所有样式
例子:
bundles.Add(new StyleBundle("~/App/bundledCSS")
.IncludeDirectory("~/App/css/other", "*.css", true)
.IncludeDirectory("~/App/css/lib", "*.css", true);
解决方案
推荐阅读
- java - Java JMS - 消息侦听器和 onException
- ios - 如何在 Swift 中指定 URLRequest 中发送的数据类型?
- vhdl - FSM 测试台
- angular - 使用 Angular 路由发出 Http 请求?
- sql - Oracle SQL 认为是 1909 年,而不是 2009 年
- c# - 试图找到数组中元素的频率
- firebase - 无法将云功能部署到 Firebase。请求缺少所需的身份验证凭据
- c# - 打开没有 .sln / .csproj 文件的 C# 项目
- html - bootstrap 4 div的垂直中心和定位
- excel - 查找下一个未突出显示的单元格(非条件)