【问题标题】:Mvc bundling based on querystring基于查询字符串的 Mvc 捆绑
【发布时间】:2018-05-13 11:49:36
【问题描述】:
我遇到了一个场景,MVC 捆绑应该基于查询字符串值发生。
我正在为文件夹内的所有 *.js 使用文件夹捆绑。我的查询字符串将具有基于该名称的子文件夹名称,我只需要在该文件夹中捆绑 js 文件。因此,即使我们在运行时添加任何文件夹并在 URL 中提供该文件夹,应用程序也应该能够在文件夹中加载 js 文件。基本上是在寻找什么来读取 bundle.config 中的查询字符串并使包的文件夹名称动态
我要找的是
bundles.Add(new ScriptBundle("~/bundles/folderbundle")
.IncludeDirectory("~/JS/"+ [FoldernamefromQueryString] +", "*.js", true)
);
提前感谢所有帮助。
【问题讨论】:
标签:
asp.net-mvc
asp.net-mvc-4
bundling-and-minification
【解决方案1】:
经过几个小时的搜索,我得到了我正在寻找的东西,你必须更改 Global.asax 中的代码
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
//BundleConfig.RegisterBundles(BundleTable.Bundles);
BundleTable.EnableOptimizations = true;
}
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
var handler = Context.Handler as MvcHandler;
if (handler != null)
{
var routeData = handler.RequestContext.RouteData;
var moduleName = !string.IsNullOrWhiteSpace(HttpContext.Current.Request.QueryString["ModuleName"]) ? HttpContext.Current.Request.QueryString["ModuleName"] : "Module";
BundleConfig.RegisterBundles(BundleTable.Bundles, moduleName);
}
}
}
在 bundle.config 中
namespace RouteBundling
{
public class BundleConfig
{
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles, string moduleName = "")
{
if (moduleName == "Module")
{
bundles.Add(new ScriptBundle("~/bundles/module").IncludeDirectory("~/Scripts/Module/", "*.js", true));
}
else
{
bundles.Add(new ScriptBundle("~/bundles/module").IncludeDirectory("~/Scripts/Module/" + moduleName + "/", "*.js", true));
}
}
}
}
这几乎为我完成了这项工作。谢谢