我正在使用ASP.NET MVC 4捆绑和缩小Microsoft.AspNet.Web.Optimization命名空间中的功能(例如@Styles.Render("~/content/static/css")).
我想将它与Windows Azure CDN结合使用.
我考虑编写自定义,BundleTransform但内容尚未优化.
我还研究了在运行时解析和上传优化的流,但这对我来说就像是一个黑客,我真的不喜欢它:
@StylesCdn.Render(Url.AbsoluteContent(
Styles.Url("~/content/static/css").ToString()
));
public static IHtmlString Render(string absolutePath)
{
// get the version hash
string versionHash = HttpUtility.ParseQueryString(
new Uri(absolutePath).Query
).Get("v");
// only parse and upload to CDN if version hash is different
if (versionHash != _versionHash)
{
_versionHash = versionHash;
WebClient client = new WebClient();
Stream stream = client.OpenRead(absolutePath);
UploadStreamToAzureCdn(stream);
}
var styleSheetLink = String.Format(
"<link href=\"{0}://{1}/{2}/{3}?v={4}\" rel=\"stylesheet\" type=\"text/css\" />",
cdnEndpointProtocol, cdnEndpointUrl, cdnContainer, cdnCssFileName, versionHash …Run Code Online (Sandbox Code Playgroud) cdn azure asp.net-mvc-4 bundling-and-minification asp.net-optimization
我想更改从捆绑请求发送的缓存标头.目前它是变化的,User-Agent但我不希望它,有没有办法更改捆绑请求发送的标头?
快速查看System.Web.Optimization程序集后,我可以看到标题设置在Bundle.SetHeaders一个私有静态函数中,所以我认为它不可能,尽管我希望被证明是错误的.
c# asp.net asp.net-mvc-4 bundling-and-minification asp.net-optimization
你会如何为asp.net mvc区域进行资源捆绑?这是否受ASP.NET MVC框架的限制,就像AreaRegistrationfor routes一样?
我可以做一个BundleConfig类区域内,并从全球称之为BundleConfig内部App_Start文件夹,但这种感觉好好尝试一下对我好.
我找不到有关此主题的任何信息.任何帮助我们的意见表示赞赏.
从一个全新的ASP.NET MVC 4 Beta Web应用程序,我正在重新安排我的文件夹以匹配Rob Conery的VidPub.Web示例
具体来说,这意味着内容的最终目录结构如下所示
VidPub.Web
|
---Public
|
|---Images
|--- *.png
|---javascripts
|--- *.js
|---stylesheets
|----themes
|---base
|--- images
|--- *.css
|----site.css
但是,当我更改以下行时 _Layout.cshtml
<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(
"~/Content/css")"
rel="stylesheet"
type="text/css" />
<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(
"~/Content/themes/base/css")"
rel="stylesheet"
type="text/css" />
<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(
"~/Scripts/js")"></script>
Run Code Online (Sandbox Code Playgroud)
至
<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(
"~/Public/stylesheets/css")"
rel="stylesheet"
type="text/css" />
<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(
"~/Public/stylesheets/themes/base/css")"
rel="stylesheet"
type="text/css" />
<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(
"~/Public/javascripts/js")"></script>
Run Code Online (Sandbox Code Playgroud)
在最终的HTML中,我最终没有任何结果.
<link rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" />
<script></script>
Run Code Online (Sandbox Code Playgroud)
为什么不在新位置拾取文件?
我刚刚将ASP.NET MVC 3项目迁移到MVC 4/.NET 4.0,并安装了NuGet包Microsoft.AspNet.Web.Optimization以支持CSS和JavaScript的捆绑和缩小.我几乎有捆绑/缩小工作,问题是它始终启用.即使应用程序处于调试模式(如在Web.config中配置),所有JavaScript包含都会缩小.从下面的XML代码段中可以看到,在Web.config中启用了调试模式:
<system.web>
<compilation debug="true" targetFramework="4.0">
...
</compilation>
...
</system.web>
Run Code Online (Sandbox Code Playgroud)
我的捆绑配置的摘录:
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
...
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-1.*",
"~/Scripts/jquery.form.js",
"~/Scripts/jquery.format.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/Site.css"));
...
}
}
Run Code Online (Sandbox Code Playgroud)
CSS/JavaScript包含在HTML中呈现,例如:
<link href="/content/css" rel="stylesheet" type="text/css">
<script src="/bundles/jquery" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
有没有人知道为什么在我的情况下启用缩小?我不知道我在这里失踪了什么.要解决我创建了一个测试ASP.NET MVC 4的互联网应用,并可以验证CSS/JavaScript的并没有在调试模式下获得精缩为这个项目.
编辑:
在我的_Layout.cshtml文件中,我像这样渲染样式/脚本:
@Styles.Render("content/css")
@Scripts.Render("bundles/jquery")
Run Code Online (Sandbox Code Playgroud)
感谢Hao,我意识到我忘了用"〜/"作为包名称的前缀.
asp.net-mvc-4 bundling-and-minification asp.net-optimization
我为谷歌地图使用了一个jQuery库,它取决于首先加载的谷歌脚本.我希望能够在包中包含这两个:
bundles.Add(new ScriptBundle("myfoobundle").Include(
"http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places",
"~/scripts/jquery.fooplugin-{version}.js"
));
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用(抛出一个抱怨第一个字符串的异常).有人可能会说这不起作用,因为绝对URL并不意味着缩小/捆绑.
但是当前的方法很麻烦,因为我需要确保依赖关系是正确的,并且发生在不同的地方(捆绑代码中的问题的一半,视图中的另一半).
如上所述,拥有一步解决方案会很高兴.我在这方面有什么选择吗?
更新:
解决有关使用CDN作为解决方案的意见:如果我指定bundles.UseCdn = true它没有效果,我仍然得到例外The URL 'http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places' is not valid. Only application relative URLs (~/url) are allowed.另外我不确定这样做的含义是什么,因为我已经使用了对jQuery等的CDN支持,所以不确定这会如何与我的用例冲突.
asp.net-mvc asp.net-mvc-4 bundling-and-minification asp.net-optimization
我需要能够暂时禁用单个请求的捆绑和缩小,以便调试JavaScript和CSS问题.我想在运行时通过向QueryString添加一个参数来执行此操作,如此...
http://domain.com/page?DisableOptimizations=true
Run Code Online (Sandbox Code Playgroud)
这是我正在考虑的方法.
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Enable for every request
BundleTable.EnableOptimizations = true;
// Disable for requests that explicitly request it
bool disable;
bool.TryParse(Context.Request.QueryString["DisableOptimizations"], out disable);
if (disable)
{
BundleTable.EnableOptimizations = false;
}
}
Run Code Online (Sandbox Code Playgroud)
c# asp.net asp.net-mvc bundling-and-minification asp.net-optimization
我不想缩小我在ASP .NET MVC 4解决方案中使用的所有文件.例如,我在BundleConfig.cs中有这个:
bundles.Add(new StyleBundle("~/CSS/bootstrap").Include(
"~/Content/Bootstrap/body.css",
"~/Content/Bootstrap/bootstrap-responsive.css",
"~/Content/Bootstrap/bootstrap-mvc-validation.css",
"~/Content/Bootstrap/bootstrap-theme.css",
"~/Content/Bootstrap/bootstrap.css"
));
...
Run Code Online (Sandbox Code Playgroud)
为了缩小它,我当然使用:
BundleTable.EnableOptimizations = true;
Run Code Online (Sandbox Code Playgroud)
所以它很棒.
但是现在,除了一个捆绑包之外,我怎么能缩小我的所有文件?我有一个捆绑的问题,删除了一些CSS类,并希望不缩小这一个.
任何帮助将不胜感激.
我想知道是否有人可以帮助我使用MVC 4附带的新优化命名空间进行捆绑和缩小.我有一个Multitenant应用程序,我想根据每个用户的设置决定应该加载哪些js文件.一种方法是预先创建所有捆绑包并根据用户的设置更改resolvebundleurl的虚拟路径,但这感觉不是真正正确的方式.另外,我在基于用户设置的cshtml视图中有动态css,我想在运行时缩小它.
有什么建议?在其他问题中我也看到很多反应来检查Requestreduce,但它们都来自同一个用户.
处理这两种情况的最佳方法是什么?
提前致谢!
asp.net optimization bundle asp.net-mvc-4 asp.net-optimization
我有以下完美运行的代码:
bundles.Add
(
new ScriptBundle("~/bundles/scripts")
.Include
(
"~/Scripts/jquery-{version}.js"
,"~/Scripts/jquery-ui-{version}.js"
,"~/Scripts/jquery.unobtrusive*"
,"~/Scripts/jquery.validate*"
,"~/Scripts/fullcalendar-1.5.4/fullcalendar.js"
, "~/Scripts/DataTables-1.9.3/media/js/jquery.dataTables.js"
)
);
Run Code Online (Sandbox Code Playgroud)
但后来我想使用{version}通配符来使用它的所有漂亮功能:
bundles.Add
(
new ScriptBundle("~/bundles/scripts")
.Include
(
"~/Scripts/jquery-{version}.js"
,"~/Scripts/jquery-ui-{version}.js"
,"~/Scripts/jquery.unobtrusive*"
,"~/Scripts/jquery.validate*"
, "~/Scripts/fullcalendar-{version}/fullcalendar.js"
, "~/Scripts/DataTables-{version}/media/js/jquery.dataTables.js"
)
);
Run Code Online (Sandbox Code Playgroud)
和BANG,例外,它不起作用,我做错了什么?
编辑
异常信息: System.ArgumentException:
目录不存在.参数名称:directoryVirtualPath
堆:
[ArgumentException:目录不存在.Имяпараметра:directoryVirtualPath]
System.Web.Optimization.Bundle.Include(String [] virtualPaths)+40
Aerostar.BundleConfig.RegisterBundles(BundleCollection包)在c:\ Users\Denis\Documents\Visual Studio 2012\Projects\Aerostar\Aerostar中\ App_Start\BundleConfig.cs:21
Aerostar.MvcApplication.Application_Start()在c:\ Users\Denis\Documents\Visual Studio 2012\Projects\Aerostar\Aerostar\Global.asax.cs:24[HttpException(0x80004005):目录不存在.Имяпараметра:directoryVirtualPath]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的上下文中,一个HttpApplication应用程序)9859725个
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的appContext,HttpContext的上下文中,MethodInfo的[]处理程序)118
System.Web.HttpApplication.InitSpecial( HttpApplicationState状态,MethodInfo []处理程序,IntPtr appContext,HttpContext上下文)+172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext,HttpContext context)+336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)+296[HttpException(0x80004005):目录不存在.Имяпараметра:directoryVirtualPath]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+9873912 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)+101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context)+254