Broken CSS Link When Published in Visual Studio 2017

Viewed 1091

I have problem when publishing a Visual Studio 2017, C#, ASP.Net MVC 5 project. I do publish with following settings :

enter image description here enter image description here

Problem with the CSS content link, for example bootstrap :

@font-face{font-family:'Glyphicons Halflings';src:url(../fonts/glyphicons-halflings-regular.eot);

If the bootstrap.min.css located in localhost/bower_components/css/bootstraps/bootstrap.min.css, that css will displayed as localhost/fonts/glyphicons-halflings-regular.eot at client.

If I just copy paste all project without publish it to the server, the link will displayed correctly as localhost/bower_components/css/fonts/glyphicons-halflings-regular.eot

This is happens for all css including images and other file in css.

4 Answers

With what is available of code knowledge in your question and what shown and explained. I can see in your code your relative path has the issue.

You are using ../ change it to ./ or ~/. That should fix the issue.

By using ../ you are stepping the css path over.

In the {YourProjectName}\App_Start\BundleConfig.Cs

Add Instance of CssRewriteUrlTransform Class when adding Bootstrap Bundle Like This:

public static void RegisterBundles(BundleCollection bundles)
{
     StyleBundle bootCss = new StyleBundle("~/styles/bootstrap");
                      bootCss.Include("~/Content/bootstrap/styles/bootstrap.css", 
                      new CssRewriteUrlTransform());
     bootCss.Include("~/Content/bootstrap/styles/bootstrap-theme.min.css", new CssRewriteUrlTransform());
     bundles.Add(bootCss);
}

Build action set to None by Default so you need to change it to Content.

Right click in affected file > properties > set Build Action to Content

I hope this can help

I found that some of my newly added files weren't publishing even though everything was set up correctly. I closed and reopened VS 2019 to find that the newly added files weren't added to the project. I remember a while ago the occasional need to go to File > Save All.

This is a simple solution, but hard to see when it's the issue.

Related