Bundling with MVC4.5 not working after publish - asp.net-mvc

I trying to bundle scripts and styles bootstrap for my application. My debug is working But when I publish it, don't load scripts and styles.
I add a BundleConfig (BootstrapBundleConfig)
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/js").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery-ui-1.10.4.custom.js",
"~/Scripts/jquery-migrate-{version}.js",
"~/Scripts/bootstrap.js",
"~/Scripts/gridmvc.js",
"~/Scripts/gridmvc.lang.fa.js",
"~/Scripts/jquery.validate.js",
"~/scripts/jquery.validate.unobtrusive.js",
"~/Scripts/jquery.validate.unobtrusive-custom-for-bootstrap.js",
"~/scripts/entitypicker.js",
"~/scripts/js-persian-cal.js",
"~/scripts/json2.js",
"~/scripts/bootbox.js",
"~/Scripts/jalali.js",
"~/Scripts/calendar.js",
"~/Scripts/calendar-setup.js",
"~/Scripts/lang/calendar-fa.js"
));
bundles.Add(new StyleBundle("~/content/css").Include(
"~/Content/bootstrap.css",
"~/Content/bootstrap-responsive.css",
"~/Content/bootstrap-mvc-validation.css",
"~/Content/themes/ui-lightness/jquery-ui.css",
"~/Content/js-persian-cal.css",
"~/Content/entitypicker.css",
"~/Content/gridmvc.css",
"~/Content/aqua/theme.css",
"~/Content/calendar-system.css"
));
}
add register this in Global
BootstrapSupport.BootstrapBundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
and set styles/script in _layaout
<link href="#Styles.Url("~/content/css")" rel="stylesheet" />
#Scripts.Render("~/js")
But when publish it, and open View Page Source
<link href="/Content/css?v=65vDyivXbF9ucPBBLls9CVnwUcCNass7hOMNUEXbN-I1" rel="stylesheet" />
and when open this file get error in first line.
/* Minification failed. Returning unminified contents.
(962,1): run-time error CSS1019: Unexpected token, found '#charset'
(962,10): run-time error CSS1019: Unexpected token, found '"UTF-8"'
(962,17): run-time error CSS1019: Unexpected token, found ';'
(994,1): run-time error CSS1019: Unexpected token, found '#-webkit-keyframes'
I search in google and used different methods, but the error is not resolved.
add all style to a special path (Content/Them/Bootstrap) and use new StyleBundle("~/content/Them/Bootstrap")
use BundleTable.EnableOptimizations = true;
other..

This usually occurs if your css files are not well formatted, put your css code to test using http://csslint.net/.
Also instead of using this line
<link href="#Styles.Url("~/content/css")" rel="stylesheet" />
you could use this
#Styles.Render("~/content/css")
If you want to debug this without publishing you could add this line to your RegisterBundles method of your BundleConfig:
BundleTable.EnableOptimizations = true;

This is the culprit #charset "UTF-8". Search through your css files and see if you can find it. This needs to be at the top of the bundled css file. i.e. At the top of the first CSS file in the bundle list. If it is not it will fail to minify.
It is often used when there are Unicode characters in the CSS file. And this is normally because of the use of an icon font like font awesome. But if you don't need it in your CSS file, then just take it out.

Your css has some 'problems' when minifying. this can be because of bad css or bad optimization.
Try to validate your CSS and see if you can spot the culprit.
For a workaround use BundleTable.EnableOptimizations = false which won't minify the css.
To 'debug' this on local host, change your web config and remove the attribute debug=true (this will make your css minify on your dev machine)
Also having different folder base on your bundle will make any relative url (like a background image) on your stylesheet to not work.

https://stackoverflow.com/a/19544226/2440976 -This is a dup question I believe - Here is likely the simple answer (It worked for me!)
(From answer) IIS Config>Authentication>RightClickOn Anonymous Auth>Click Edit> Check Application pool identity

Related

is it possible to include Scripts and Styles in same Bundle ?(asp.net mvc5)

Attempting to use css and js files with same virtualpath bundle name
1 - is it possible ? (tried:but failed. cant define same virtual path name both for script and style)
2 - it it possible to builtup a ScriptAndStyleBundle together included with a mixed bundle ?
Just because I wantto use same name both for css and js.
//in BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/doMenu")
.Include("~/Plugins/doMenu/files/js/doMenu.js")
);
bundles.Add(new StyleBundle("~/bundles/doMenu")
.Include("~/Plugins/doMenu/files/css/doMenu.css")
);
//in _PartialLayoutMenu.cs
#Scripts.Render("~/bundles/doMenu")
#Styles.Render("~/bundles/doMenu")
result is:
<!--MENU LAYOUT-->
<script src="/Plugins/doMenu/files/css/doMenu.css"></script>
<link href="/Plugins/doMenu/files/css/doMenu.css" rel="stylesheet"/>
<!--/MENU LAYOUT-->
any clue ? or I want such a useless mind ?(thanks)
I'm using Cassette, which does support this feature.
Configuration:
bundles.Add<ScriptBundle>(BundleNames.Grid,
new[] {"~/Scripts/Grid.js"},
bundle => bundle.AddReference("~/" + BundleNames.Base));
bundles.Add<StylesheetBundle>(BundleNames.Grid,
new[] {"~/Content/Grid.less"});
BundleNames is a helper class I have with constant strings.
Reference bundles in views:
#{
Bundles.Reference(BundleNames.Grid);
}
As you'd expect, this will include all CSS and JS, in the correct order.
I'd also mention that I've started using Cassette before ASP.Net had bundle management and I'm very happy with it. I didn't find any interesting feature ASP.Net had that I'm missing.
Bundle names should be unique within the both styles and scripts. And since they are entirely different types you cannot mix them into a single bundle because then you wouldn't know how to reference it in the DOM: whether you should use a <script> or a <style> tag. Unfortunately what you are asking for is not possible.

PicketLink protection stops JSF 2.2 resource contract from being loaded

I have a JSF 2.2 webapp with a contract and several pages, located directly in the WebContent folder. The contract consists of an image, a template file template.xhtml and a css file global.css. So far everything is working as expected.
Now I want to use PicketLink for user authentication and authorization and have followed a tutorial (http://www.ocpsoft.org/security/simple-java-ee-jsf-login-page-with-jboss-picketlink-security/), but when accessing my pages the image and css files are unable to be loaded, only the template applies, so my page has no CSS styles applied at all and in the Firefox Inspector there is a line that reads (translated from German): "Stylesheet http://localhost:8080/MyTestProject/login.xhtml wasn't loaded because its MIME type is "text/html" and not "text/css"".
After replacing
builder.http().allPaths().authenticateWith().form()... and so on
in the HttpSecurityConfiguration class with
builder.http().allPaths().unprotected()
the image and css can be loaded again.
I have tried the following (and some other paths) but it did not solve the problem:
.forPath("/contracts/*").unprotected();
How can I exclude the contracts folder from the PicketLink protection?
Here is my complete HttpSecurityConfiguration class:
#ApplicationScoped
public class HttpSecurityConfiguration {
public void onInit(#Observes SecurityConfigurationEvent event) {
SecurityConfigurationBuilder builder = event.getBuilder();
builder
.http()
.allPaths()
.authenticateWith()
.form()
.loginPage("/login.xhtml")
.errorPage("/loginError.xhtml")
.restoreOriginalRequest()
.forPath("/logout")
.logout()
.redirectTo("/index.xhtml")
.forPath("/index.xhtml")
.unprotected()
// .forPath("/contracts/*")
// .unprotected()
;
}
}
EDIT
In reply to the comment from Kukeltje, I include the CSS in the template with
<h:head>
<title><ui:insert name="title">MyTestProject</ui:insert></title>
<h:outputStylesheet name="global.css" />
</h:head>
and the image with
<h:graphicImage class="feature" name="logo-main.png" width="900" height="270" />
I also tried to include javax.faces.resource as unprotected, still not working though.
EDIT #2
The following is also not working, I got the idea from the documentation (PicketLink Reference Chapter 12.2):
.forPath("/*.png").unprotected()
.forPath("/*.css").unprotected()
I was able to solve my problem with the following security configuration:
.forPath("/javax.faces.resource/*.png.xhtml").unprotected()
I've seen in my Firefox Inspector that the browser tried to load the image from /MyTestProject/javax.faces.resource/logo-main.png.xhtml?con=TemplateBlue, so trying the above seemed logical and it works!

how to debug Minification failed. Returning unminified contents

I am using Asp.net MVC 4 bundler to bundle and minify my Css files.
YSlow is showing this error below
/* Minification failed. Returning unminified contents.
(1442,26): run-time error CSS1019: Unexpected token, found ':'
(1442,26): run-time error CSS1042: Expected function, found ':'
(1442,26): run-time error CSS1062: Expected semicolon or closing curly-brace, found ':'
*/
This is my bundle code,
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/site.css",
"~/Content/fullcalendar.css",
"~/Content/jquery.dropdown.css",
"~/Content/jquery.tagit.css",
"~/Content/tipsy.css"
));
Now how will i find out which css file is causing the problem ? How can i debug to find the line that is causing the problem ? site.css is the only css file that i wrote.
In case someone is still dealing with problems like this.
In the example above: (1442,26) 1442 is the line number and 26 is the character offset. HOWEVER, for this to be exact, you need remove the whole comment where this error is indicated:
/* Minification failed. Returning unminified contents.
(1442,26): run-time error CSS1019: Unexpected token, found ':'
(1442,26): run-time error CSS1042: Expected function, found ':'
(1442,26): run-time error CSS1062: Expected semicolon or closing curly-brace, found ':'
*/
Here's two possible causes:
The invalid CSS files that should be validated before bundling. here
is W3C CSS validation service to meet this purpose.
Also considering that Microsoft Optimizer reads content of target
resources for minification process, so by using some special phrases
like # sourceMappingURL=jquery.min.map in a JavaScript file or
#charset "UTF-8"; in a styleSheet file, the Minification will be
failed again. So try to remove or commentize them.
Note that by default, Bundling process can't build relative path of image resources in css or js files.
Relative Image Path Solution:
You can use the same path as bundling path like:
bundles.Add(new StyleBundle("~/Content/css/jquery-ui/bundle")
.Include("~/Content/css/jquery-ui/*.css"));
Where you define the bundle on the same path as the source files that made up the bundle, the relative path of image resources will still work( i.e. /bundle can be any name you like).
Or using new CssRewriteUrlTransform() as second parameter like:
bundles.Add(new StyleBundle("~/Content/css/bundle")
.Include("~/Content/css/*.css", new CssRewriteUrlTransform()));
filter: alpha(opacity: 0); was the line that was causing the issue. After i removed this line
i was able to minify the css file without any issue,

Minified script only in MVC4 BundleConfig

I am adding the following ScriptBundle in BundleConfig:
bundles.Add(new ScriptBundle("~/bundles/javascript").Include(
"~/Scripts/jquery-1.*",
"~/Scripts/load-image.min.js",
"~/Scripts/bootstrap.*",
"~/Scripts/bootstrap-image-gallery.*",
"~/Scripts/my.global.js"));
This is referenced at the end of my _Layout.cshtml as:
#Scripts.Render("~/bundles/javascript")
When debugging I notice that the output of this script rendering is:
<script src="/Scripts/jquery-1.8.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/bootstrap-image-gallery.js"></script>
<script src="/Scripts/my.global.js"></script>
Notice the load-image.min.js script is missing? What I want is to use that same minified script whether I'm debugging or not. Under release conditions the script is included in the bundled JS file.
I assume it's seeing the 'min', looking for an un-minified version, not finding one, then deciding what's best is to ignore it entirely. Brilliant. If I make a copy of load-image.min.js, call it load-image.js and then reference it in BundleConfig as "load-image.*" I find it is included in both configurations but what's the point of having to do that?
I assume I'm missing something here. I don't have the un-minified version and I frankly don't care about it. It's used by my Bootstrap image gallery plugin and nothing else. Any ideas out there?
This behavior has been improved (fixed) in the 1.1.0-alpha1 release. We moved all of the old default ignore list entries into a new DirectoryFilter ignore list that are only used when including search patterns like *.js which was the origional intent for this functionality. As a result this should no longer be an issue when you are including individual files explicitly.
Note: the one place this might still be an issue is if you try to include something like jquery-{version}.min.js.
There is ignoreList, which you can clear if you need, it looks like:
public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
{
if (ignoreList != null)
{
ignoreList.Ignore("*.intellisense.js");
ignoreList.Ignore("*-vsdoc.js");
ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
return;
}
else
{
throw new ArgumentNullException("ignoreList");
}
}
More details: Advanced Options of ASP.NET Bundling and Minification

Autoversioning CSS/JS in ASP.NET MVC?

So I was reading this stackoverflow post about "autoversioning" in ASP.NET MVC for CSS/JS files and was wondering what the "best" strategy is to do this.
The solution provided inserts an assembly number - which means everytime you publish - it will change EVERY SINGLE file which is not ideal because if you make modifications to just 1 *.css or *.js then it will change each and every file.
1) How can it be done just for "single files" instead of using site wide assembly using modification date or something on IIS7 ?
2) Also if I have some sort of "static" asset like - http://static.domain.com/js/123.js - how can I use rewrite to send the latest file for a request if someone has integrated this static link onto their site ?
i.e. http://static.domain.com/js/123.js is the link and when a request comes for this - check and send latest file ?
ASP.NET 4.5+ comes with a built-in bundling & minification framework
which is designed to solve this problem.
If you absolutely need a simple roll-your-own solution you can use the answer below, but I would always say the correct way is to use a bundling & minification framework.
You can modify the AssemblyInfo.cs file like so:
Change
[assembly: AssemblyVersion("1.0.0.0")]
to
[assembly: AssemblyVersion("1.0.*")]
This means that every time the project is built, it will have a new assembly version which is higher than the previous one. Now you have your unique version number.
Create an UrlHelperExtension class that will help get this information when needed in the views:
public static class UrlHelperExtensions
{
public static string ContentVersioned(this UrlHelper self, string contentPath)
{
string versionedContentPath = contentPath + "?v=" + Assembly.GetAssembly(typeof(UrlHelperExtensions)).GetName().Version.ToString();
return self.Content(versionedContentPath);
}
}
You can now easily add a version number to your views in the following manner:
<link href="#Url.ContentVersioned("style.css")" rel="stylesheet" type="text/css" />
When viewing your page source you will now have something that looks like
<link href="style.css?v=1.0.4809.30029" rel="stylesheet" type="text/css" />
UPDATE: The previous version did not work on Azure, I have simplified and corrected below. (Note, for this to work in development mode with IIS Express, you will need to install URL Rewrite 2.0 from Microsoft http://www.iis.net/downloads/microsoft/url-rewrite - it uses the WebPi installer, make sure to close Visual Studio first)
If you would like to change the actual names of the files, rather than appending a querystring (which is ignored by some proxies / browsers for static files) You can follow the following steps: (I know this is an old post, but I ran across it while developing a solution:
How to do it: Auto-increment the assembly version every time the project is built, and use that number for a routed static file on the specific resources you would like to keep refreshed. (so something.js is included as something.v1234.js with 1234 automatically changing every time the project is built) - I also added some additional functionality to ensure that .min.js files are used in production and regular.js files are used when debugging (I am using WebGrease to automate the minify process) One nice thing about this solution is that it works in local / dev mode as well as production. (I am using Visual Studio 2015 / Net 4.6, but I believe this will work in earlier versions as well.
Step 1: Enable auto-increment on the assembly when built
In the AssemblyInfo.cs file (found under the "properties" section of your project change the following lines:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
to
[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]
Step 2: Set up url rewrite in web.config for files with embedded version slugs (see step 3)
In web.config (the main one for the project) add the following rules in the <system.webServer> section I put it directly after the </httpProtocol> end tag.
<rewrite>
<rules>
<rule name="static-autoversion">
<match url="^(.*)([.]v[0-9]+)([.](js|css))$" />
<action type="Rewrite" url="{R:1}{R:3}" />
</rule>
<rule name="static-autoversion-min">
<match url="^(.*)([.]v[0-9]+)([.]min[.](js|css))$" />
<action type="Rewrite" url="{R:1}{R:3}" />
</rule>
</rules>
</rewrite>
Step 3: Setup Application Variables to read your current assembly version and create version slugs in your js and css files.
in Global.asax.cs (found in the root of the project) add the following code to protected void Application_Start() (after the Register lines)
// setup application variables to write versions in razor (including .min extension when not debugging)
string addMin = ".min";
if (System.Diagnostics.Debugger.IsAttached) { addMin = ""; } // don't use minified files when executing locally
Application["JSVer"] = "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString().Replace('.','0') + addMin + ".js";
Application["CSSVer"] = "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString().Replace('.', '0') + addMin + ".css";
Step 4: Change src links in Razor views using the application variables we set up in Global.asax.cs
#HttpContext.Current.Application["CSSVer"]
#HttpContext.Current.Application["JSVer"]
For example, in my _Layout.cshtml, in my head section, I have the following block of code for stylesheets:
<!-- Load all stylesheets -->
<link rel='stylesheet' href='https://fontastic.s3.amazonaws.com/8NNKTYdfdJLQS3D4kHqhLT/icons.css' />
<link rel='stylesheet' href='/Content/css/main-small.#HttpContext.Current.Application["CSSVer"]' />
<link rel='stylesheet' media='(min-width: 700px)' href='/Content/css/medium.#HttpContext.Current.Application["CSSVer"]' />
<link rel='stylesheet' media='(min-width: 700px)' href='/Content/css/large.#HttpContext.Current.Application["CSSVer"]' />
#RenderSection("PageCSS", required: false)
A couple things to notice here: 1) there is no extension on the file. 2) there is no .min either. Both of these are handled by the code in Global.asax.cs
Likewise, (also in _Layout.cs) in my javascript section: I have the following code:
<script src="~/Scripts/all3bnd100.min.js" type="text/javascript"></script>
<script src="~/Scripts/ui.#HttpContext.Current.Application["JSVer"]" type="text/javascript"></script>
#RenderSection("scripts", required: false)
The first file is a bundle of all my 3rd party libraries I've created manually with WebGrease. If I add or change any of the files in the bundle (which is rare) then I manually rename the file to all3bnd101.min.js, all3bnd102.min.js, etc... This file does not match the rewrite handler, so will remain cached on the client browser until you manually re-bundle / change the name.
The second file is ui.js (which will be written as ui.v12345123.js or ui.v12345123.min.js depending on if you are running in debug mode or not) This will be handled / rewritten. (you can set a breakpoint in Application_OnBeginRequest of Global.asax.cs to watch it work)
Full discussion on this at: Simplified Auto-Versioning of Javascript / CSS in ASP.NET MVC 5 to stop caching issues (works in Azure and Locally) With or Without URL Rewrite (including a way to do it WITHOUT URL Rewrite)
1)
Use file modification time instead. Here's an example:
public static string GeneratePathWithTime(string cssFileName)
{
var serverFilePath = server.MapPath("~/static/" + cssFileName);
var version = File.GetLastWriteTime(serverFilePath).ToString("yyyyMMddhhmmss");
return string.Format("/static/{0}/{1}", version, cssFileName);
}
This will generate a path like "/static/201109231100/style.css" for "style.css" (assuming the your style.css is located in the static directory).
You'll then add a rewrite rule in IIS to rewrite "/static/201109231100/style.css" to "/static/style.css". The version number will only be changed when the css file has been modified and only applies to modified files.
2)
You can handle the request to 123.js via an HttpModule and send the latest content of it, but I don't think you can guarantee the request gets the latest version. It depends on how the browser handles its cache. You can set an earlier expiration time (for example, one minute ago) in your response header to tell the browsers to always re-download the file, but it's all up to the browser itself to decide whether to re-download the file or not. That's why we need to generate a different path for our modified files each time we updated our files in your question 1), the browser will always try to download the file if the URL has never been visited before.
I wrote a Url Helper which does the CacheBusting for me.
public static string CacheBustedContent(this UrlHelper helper, string contentPath)
{
var path = string.Empty;
if (helper.RequestContext.HttpContext.Cache["static-resource-" + contentPath] == null)
{
var fullpath = helper.RequestContext.HttpContext.Server.MapPath(contentPath);
var md5 = GetMD5HashFromFile(fullpath);
path = helper.Content(contentPath) + "?v=" + md5;
helper.RequestContext.HttpContext.Cache.Add("static-resource-" + contentPath, path, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0), System.Web.Caching.CacheItemPriority.Default, null);
}
else
{
path = helper.RequestContext.HttpContext.Cache["static-resource-" + contentPath].ToString();
}
return path;
}
You could replace the GetMD5HashFromFile() with CRC or any other sort of call which generates a unique string based on the contents or last-modified-date of the file.
The downside is this'll get called whenever the cache is invalidated. And if you change the file on live somehow, but don't reset the application pool, you'll probably need to touch the web.config to get it to reload correctly.
You might want to have a look at Dean Hume's Blogpost MVC and the HTML5 Application Cache. In that post, he points out an elegant way of automatically handling versioning per request, using a class library of #ShirtlessKirk:
#Url.Content("~/Content/Site.css").AppendHash(Request)
This question is really old now, but if anyone stumbles upon it, here's to my knowledge the current state of the art:
In ASP.NET Core you can use TagHelpers and simply add the asp-append-version attribute to any <link> or <script> tag:
<script src="~/js/my.js" asp-append-version="true"></script>
For both ASP.NET Core and Framework there is a NuGet Package called WebOptimizer (https://github.com/ligershark/WebOptimizer). It allows for both bundling and minification, and will also append a content-based version string to your file.
If you want to do it yourself, there is the handy IFileVersionProvider interface, which you can get from your IServiceProvider in .NET Core:
// this example assumes, you at least have a HttpContext
var fileVersionProvider = httpContext.RequestServices.GetRequiredService<IFileVersionProvider>();
string path = httpContext.Content("/css/site.css");
string pathWithVersionString = fileVersionProvider.AddFileVersionToPath(httpContext.Request.PathBase, path);
For .NET Framework, you can get the FileVersionProvider source from here: https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Razor/src/Infrastructure/DefaultFileVersionProvider.cs
You will have to do some work, like replacing the Cache with MemoryCache.Default or a ConcurrentDictionary or something, but the 'meat' is there.

Resources