@ Styles.Render does not work on server

2

Dear friends of Stackoverflow, I have the following problem:

I am developing an ASP.NET MVC 4 application, which I have uploaded to a Azure server.

The issue is that when I'm working locally, I compile and run the application on the local server I see all the styles properly and everything works great, but when I upload the application to Azure, I do not Take the rendered styles.

For example a button, I have a style sheet in the following path "~/Tema/pages/css/login-5.min.css" and to load it in the page I use @Styles.Render("~/CssTemaLogin") , in the head of the view ( Index.cshtml ), but I do not take the style, when I do a revision with chrome, it does not appear inside the source folders and in the console it tells me that it did not find the file.

Can anyone have any idea why this error is occurring?

This is the code of the bundle.Config where I have the routes:

 bundles.Add(new StyleBundle("~/CssTemaLogin").Include(
                    "~/Tema/global/css/components-rounded.min.css",
                    "~/Tema/global/css/plugins.min.css",
                    "~/Tema/global/plugins/select2/css/select2.min.css",
                    "~/Tema/global/plugins/select2/css/select2-bootstrap.min.css",
                    "~/Tema/pages/css/login-5.min.css"
                ));
    
asked by Fernando Campos 18.03.2017 в 19:34
source

3 answers

2

Dear, I leave this information after two weeks of hitting the keyboard I found why the images of the css classes that were rendered with Style.Bundle were not displayed, I answer my question so that in case someone else is find this problem, I can solve it.

The first thing was to understand how that class works that renders your css through the function Include. What this function does is create a css with all the files that you give it. That is, if I have:

bundles.Add(new StyleBundle("~/CssTemaLogin").Include(
        "~/Tema/global/css/components-style-1.min.css",
        "~/Tema/global/css/components-style-2.min.css",
        "~/Tema/global/css/components-style-3.min.css",
        "~/Tema/global/css/components-style-4.min.css"
        ));

On the server I will create a style document in the root path and it will be called "CssTemaLogin", now the problem of routes is generated when one of the .css files that I included in the bundles, refers to an image, since that property, will stop being in path that is mentioned in the Include and will be in the root, or where we indicate to the StyleBundle that it is positioned. An example to make this clearer, if we have the following in the document "~ / Topic / global / css / components-style-1.min.css":

cabecera {
          background-image: url("../images/cabecera.jpg");
}

What we see above is a css class, which is indicating to the file that a level should go out and enter the images folder and select the image cabecera.jpg If you look at the position of the style sheet "~ / Theme / global / css / components-style-1.min.css", you are telling it to exit the css folder and enter the images folder, however, and This is where the problem is generated, when that style sheet is part of a StyleBundle, it is no longer in the path that the file mentions, that is, it will try to go up a level from where the css file is, which as we said before to be included in the StyleBundle will now be at the root of the server and not in the path specified in the css document and will not find the image.

The solution: Change the url of the images by giving them the whole path, in this way instead of looking like this:

cabecera {
          background-image: url("../images/cabecera.jpg");
}

will look like this:

cabecera {
          background-image: url("Tema/global/images/cabecera.jpg");
}

I hope that this solution will help someone else and I hope to have been clear with my explanation.

Greetings and thanks to everyone who helped me with this question.

    
answered by 28.03.2017 / 19:10
source
1

Try changing the name of the bundle to one with a different path prefix to the root. For example:

bundles.Add(new StyleBundle("~/bundles/CssTemaLogin").Include(
            "~/Tema/global/css/components-rounded.min.css",
            ... ));

Probably the error is due to the need to indicate a virtual path , instead of a real path of the server. In production, this route will serve the minified and combined files included in the bundle. In debug mode, the system will serve the separate files from their original locations, and that's why you have correct behavior.

It is advisable to do the same with the other bundles of your application. Obviously you should change the code that injects the bundles in your views to reflect that change.

    
answered by 19.03.2017 в 11:07
1

Something that has happened to me with this is that the server decides to ignore the files .min.* , I guess for the sake of doing the minification on their own. Try renaming those .min or uploading the originals and activating the minification in the server with:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(…);
   … 
    BundleTable.EnableOptimizations = true;
}
    
answered by 20.03.2017 в 23:28