实际上 Bundle 类不支持绝对路径。我在这里打开了一个功能请求。
我已经使用自定义 VirtualPathProvider 和控制器解决了这个问题。但它是非常脆弱的实现。您可以根据需要进行修改。
public class ModuleScriptProvider : VirtualPathProvider
{
public static IBundleModule GetModulePath(string moduleName)
{
//return the absolute or relative path.
}
public static void ParseVirtualPath(string virtualPath, out string moduleName, out string path)
{
var parts = virtualPath.Replace("~/", "").Split('/').Where(p => !string.IsNullOrEmpty(p)).Skip(1).ToList();
if (parts.Count < 2 || !virtualPath.EndsWith(".js"))
throw new FileNotFoundException("Script file doesn't found in the module script directory", virtualPath);
moduleName = parts.First(); // ~/ModuleScript/ModuleName/....
path = string.Join(@"\", parts.Skip(1));
}
public static string MapPathMethod(string virtualPath)
{
if (IsModuleScriptPath(virtualPath))
{
string moduleName;
string path;
ParseVirtualPath(virtualPath, out moduleName, out path);
var moduleDir = GetModulePath(moduleName); //absolute path
if (!moduleDir.EndsWith(@"\"))
moduleDir += @"\";
return moduleDir + path;
}
return HttpContext.Current.Server.MapPath(virtualPath);
}
public static bool IsModuleScriptPath(string virtualPath)
{
String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.StartsWith("~/ModuleScript/", StringComparison.InvariantCultureIgnoreCase);
}
public override bool FileExists(string virtualPath)
{
return (IsModuleScriptPath(virtualPath) || base.FileExists(virtualPath));
}
public override VirtualFile GetFile(string virtualPath)
{
if (IsModuleScriptPath(virtualPath))
return new ModuleScriptVirtualFile(virtualPath);
return base.GetFile(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (IsModuleScriptPath(virtualPath))
{
string moduleName;
string path;
ParseVirtualPath(virtualPath, out moduleName, out path);
var bundleModulePath = GetModulePath(moduleName);
return new CacheDependency(bundleModulePath);
}
return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
public override string CombineVirtualPaths(string basePath, string relativePath)
{
return base.CombineVirtualPaths(basePath, relativePath);
}
}
class ModuleScriptVirtualFile : VirtualFile
{
private readonly string _path;
private readonly string _moduleName;
public ModuleScriptVirtualFile(string virtualPath)
: base(virtualPath)
{
ModuleScriptProvider.ParseVirtualPath(virtualPath, out _moduleName, out _path);
}
public override Stream Open()
{
var moduleDir = ModuleScriptProvider.GetModulePath(_moduleName);
if (!moduleDir.EndsWith(@"\"))
moduleDir += @"\";
return new FileStream(moduleDir + _path, FileMode.Open);
}
}
控制器:
public class ModuleScriptController : Controller
{
public FileStreamResult GetFile(string path)
{
return new FileStreamResult(new ModuleScriptProvider().GetFile("~/ModuleScript/" + path).Open(), "text/javascript");
}
}
在 Global.asax Application_Start 方法中:
HostingEnvironment.RegisterVirtualPathProvider(new ModuleScriptProvider());
BundleTable.EnableOptimizations = true;
BundleTable.MapPathMethod = ModuleScriptProvider.MapPathMethod;
神速。