【发布时间】:2013-01-04 17:37:59
【问题描述】:
我是 MVC 新手。
我知道如何创建捆绑包,它很简单,而且是一个很棒的功能:
bundles.Add(new StyleBundle("~/content/css").Include(
"~/content/main.css",
"~/content/footer.css",
"~/content/sprite.css"
));
但是假设您的应用程序可以在不同的域下访问,并根据主机名使用不同的 css 提供不同的内容。
如何让捆绑包根据主机名包含不同的文件? 在我的 RegisterBundles 所在的应用程序开始(就像我开始使用的 MVC 标准互联网应用程序一样)我什至不知道主机名。
最佳做法是什么?
如果我在注册捆绑包时有可用的主机名,我可以为当前主机名选择正确的 .css 文件。 例如,我可以在应用程序开始请求上注册捆绑包,并以某种方式检查它是否已经注册,如果没有,为请求的主机名选择正确的文件并注册它?
如果是,怎么做?
编辑 1
在过去的两个小时里,我对这个话题进行了更深入的研究,让我提出我的解决方案,希望比我更擅长 MVC 的人可以纠正我的方法,如果有错误。
我换了:
@Styles.Render("~/Content/css")
与:
@Html.DomainStyle("~/Content/css")
这只是一个简单的助手:
public static class HtmlExtensions
{
public static IHtmlString DomainStyle(this HtmlHelper helper, string p)
{
string np = mynamespace.BundleConfig.RefreshBundleFor(System.Web.Optimization.BundleTable.Bundles, "~/Content/css");
if (!string.IsNullOrEmpty(np))
return Styles.Render(np);
else
{
return Styles.Render(p);
}
}
}
RefreshBundleFor 在哪里:
public static string RefreshBundleFor(BundleCollection bundles, string p)
{
if (bundles.GetBundleFor(p) == null)
return null;
string domain = mynamespace.Utilities.RequestUtility.GetUpToSecondLevelDomain(HttpContext.Current.Request.Url);
string key = p + "." + domain;
if (bundles.GetBundleFor(key) == null)
{
StyleBundle nb = new StyleBundle(key);
Bundle b = bundles.GetBundleFor(p);
var bundleContext = new BundleContext(new HttpContextWrapper(HttpContext.Current), BundleTable.Bundles, p);
foreach (FileInfo file in b.EnumerateFiles(bundleContext))
{
string nf = file.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(file.Name) + "." + domain + file.Extension;
if (!File.Exists(nf))
nf = file.FullName;
var basePath = HttpContext.Current.Server.MapPath("~/");
if (nf.StartsWith(basePath))
{
nb.Include("~/" + nf.Substring(basePath.Length));
}
}
bundles.Add(nb);
}
return key;
}
而 GetUpToSecondLevelDomain 只是从主机名中返回二级域,因此 GetUpToSecondLevelDomain("www.foo.bar.com") = "bar.com"。
怎么样?
【问题讨论】:
标签: c# asp.net-mvc-4 bundle