带有绝对路径的ASP.NET MVC Bundle

时间:2013-02-12 14:54:07

标签: asp.net-mvc-4 bundle web-optimization

我在IIS上有虚拟目录,其中包含所有javascript和css文件。是否可以使用Bundles将这些文件包含到ASP.NET MVC应用程序中。

我已经尝试过这样的事情:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include("/public/javascript/jquery-{version}.js"));

但得到了错误:

  

仅允许应用程序相对URL(〜/ url)。:

/ public / javascript是IIS上的虚拟目录。

1 个答案:

答案 0 :(得分:4)

实际上Bundle类不支持绝对路径。我打开了a feature request here.

我用自定义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;

上帝的速度。