asp.net – 如何在使用无cookie的静态内容服务器时使bundle无法管理并列出单个文件?
|
我有两个VS项目,一个用于主网站,一个用于“静态内容”网站,其中所有css,js,图像和其他静态内容将通过无cookie域存储和访问. 所以我的静态站点中有一个BundleConfig.cs,可以创建所有的bundle: public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/bundles/styles").IncludeDirectory("~/app/styles","*.css",true));
bundles.Add(new ScriptBundle("~/bundles/scripts").IncludeDirectory("~/app/src","*.js",true));
}
}
在主站点我有另一个BundleConfig.cs,我将主站点指向静态内容站点,如下所示: public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
var staticWebsite = ConfigurationManager.AppSettings["StaticWebsite"];
var versionNumber = ConfigurationManager.AppSettings["VersionNumber"];
Styles.DefaultTagFormat = string.Format("<link href='{0}{{0}}?v={1}' rel='stylesheet'/>",staticWebsite,versionNumber);
Scripts.DefaultTagFormat = string.Format("<script src='{0}{{0}}?v={1}'></script>",versionNumber);
}
}
现在我可以使用@ Styles.Render(“/ bundles / styles”)和@ Scripts.Render(“/ bundles / scripts”)这样渲染,就像我想要的那样,并且效果很好: <link href='http://mycookielessdomain.com/bundles/styles?v=1.0.0.0' rel='stylesheet'/> <script src='http://mycookielessdomain.com/bundles/scripts?v=1.0.0.0'></script> 我遇到的问题是无论debug = true与否,内容总是被缩小和捆绑.即使我在BundleConfig.cs文件中使用BundleTable.EnableOptimization = false,@ Styles.Render()和@ Scripts.Render()仍然只渲染一个标记并引用缩小的内容. 我知道主站点不知道静态内容站点中捆绑的单个文件,但我希望有一些方法可以在主站点BundleConfig中手动指定这些路径,以便Render()方法可以当优化关闭时单独列出它们……如果我能让它们关闭,那就是. 解决方法所以我能够通过添加一个自定义的VirtualPathProvider来实现这个功能,它可以在单个文件的静态内容项目中进行主项目搜索.在DEBUG模式下,文件将单独列出.在RELEASE模式下,将引用缩小的包.public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
var staticWebsite = ConfigurationManager.AppSettings["StaticWebsite"];
var versionNumber = ConfigurationManager.AppSettings["VersionNumber"];
Styles.DefaultTagFormat = string.Format("<link href='{0}{{0}}?v={1}' rel='stylesheet'/>",versionNumber);
#if DEBUG
// Includes files from the static content project so they can be listed individually if in DEBUG mode.
BundleTable.VirtualPathProvider = new StaticContentVirtualPathProvider();
bundles.Add(new StyleBundle("~/bundles/styles").IncludeDirectory("~/app/styles",true));
#endif
}
}
这是我的自定义VirtualPathProvider: public class StaticContentVirtualPathProvider : VirtualPathProvider
{
// Modify this to be the relative path from your main project to your static content project.
private const string StaticContentRelativePath = @"....MyStaticContentProjectFolder";
public static string GetStaticContentPath(string virtualPath,bool isDirectory = false)
{
virtualPath = virtualPath.Replace('/','').Replace("~","");
if (isDirectory && !virtualPath.EndsWith("")) virtualPath += "";
return HttpRuntime.AppDomainAppPath + StaticContentRelativePath + virtualPath;
}
public override bool FileExists(string virtualPath)
{
return File.Exists(GetStaticContentPath(virtualPath));
}
public override bool DirectoryExists(string virtualDir)
{
return Directory.Exists(GetStaticContentPath(virtualDir));
}
public override VirtualFile GetFile(string virtualPath)
{
return new StaticContentVirtualFile(virtualPath);
}
public override VirtualDirectory GetDirectory(string virtualDir)
{
return new StaticContentVirtualDirectory(virtualDir);
}
private class StaticContentVirtualFile : VirtualFile
{
public StaticContentVirtualFile(string virtualPath)
: base(virtualPath)
{
this.virtualPath = virtualPath;
}
private readonly string virtualPath;
public override Stream Open()
{
return new FileStream(StaticContentVirtualPathProvider.GetStaticContentPath(virtualPath),FileMode.Open);
}
}
internal class StaticContentVirtualDirectory : VirtualDirectory
{
public StaticContentVirtualDirectory(string virtualPath)
: base(virtualPath)
{
}
public override IEnumerable Files
{
get
{
var filePaths = Directory.GetFiles(GetStaticContentPath(this.VirtualPath,true));
var files = filePaths.Select(filePath => new StaticContentVirtualFile(this.VirtualPath + Path.GetFileName(filePath))).ToList();
return files;
}
}
public override IEnumerable Directories
{
get
{
var subDirectoryPaths = Directory.GetDirectories(GetStaticContentPath(this.VirtualPath,true));
var dirs = subDirectoryPaths.Select(subDirectoryPath => new StaticContentVirtualDirectory(this.VirtualPath + Path.GetFileName(subDirectoryPath))).ToList();
return dirs;
}
}
public override IEnumerable Children { get { throw new NotImplementedException(); } }
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 为什么WebViewPage在MVC3中是抽象的?
- asp.net-core – 如何在ASP.NET 5中使用“旧”依赖项
- 如何在ASP.NET项目中正确引用JavaScript文件?
- asp.net – 什么是Kestrel(vs IIS/Express)
- asp.net-mvc – 窗口身份验证在MVC4中不起作用
- asp.net – 即使在IIS的web.config中使用标签后,也会出现重
- 用户锁定.net 4.5.1 ASP.NET MVC 5
- asp.net – 如何增加IIS 7.0上的线程池线程
- asp.net – 在更改密码时从所有浏览器注销用户
- .net – 如何动态清除用户控件中的所有控件?
- 身份更改GUID为int
- asp.net-mvc – 在同一个视图文件夹中的RenderPa
- .net – 实现UserManager以使用自定义类和存储过
- asp.net-mvc – MVC Preview 5 – 将视图呈现为字
- ASP.NET MVC 1是否与ASP.NET MVC 2兼容?
- ASP.NET Core中的Startup.cs中的asp.net-core –
- asp.net-mvc – 不需要必需属性的bool属性
- asp.net-mvc – ASP.NET MVC – 保持控制器薄(太
- asp.net – 自定义控件变为通用的“UserControl”
- asp.net-mvc – Html.CheckBox即使模型值为true也
