C# MVC StyleBundle and ScriptBundle do not render correct paths if project is run in subdirectory

772 views Asked by At

I have this code in BundleConfig.cs which simply includes a script

bundles.Add(new ScriptBundle("~/bundles/ui-scripts").Include(
                        "~/Scripts/ui-scripts.js"));

On my locale machine (http://localhost:57210/) this renders out as

<script src="/Scripts/ui-scripts.js"></script>

Locally all works fine.

The problem is, we have testing server that runs on Team City where the project is run under subfolder (http://testserver.com/myApp/), but the rendered output is the same

<script src="/Scripts/ui-scripts.js"></script>

which causes ui-scripts.js fail to load (404) because it tries to load the script from http://testserver.com/Scripts/ui-scripts.js which of course is not there, because the script is in http://testserver.com/myApp/Scripts/ui-scripts.js

I expected the framework to realize that ~ points to the root of the project which in my case is http://testserver.com/myApp/

If I enable optimizations

BundleTable.EnableOptimizations = true;

The output is rendered optimized, but still from the wrong location, again, missing the myApp subfolder

<script src="/bundles/ui-scripts?v=mRQZjAGbYccSwhcFn4iouztfiK3Xjwik5TtdvD58fHM1"></script>

Additionally , if I don't use bundles but instead in the _Layout.cshtml where I want the script to appear, write in the code like this

<script src="~/Scripts/ui-scripts.js"></script>

Then the paths are recognised correctly on the server to

<script src="/myApp/Scripts/ui-scripts.js"></script>

Additionally, If I create subdirectory on my localhost and store application there, the paths are resolved correctly.

The same problem goes for StyleBundle. Any idea what I could try?

1

There are 1 answers

0
CyberProdigy On

I found the problem. There was problem with configuration for IIS on the testing server. The project actually had it's own subdomain assigned to it, and was not supposed to be run from subdirectory. Fiddling with IIS settings solved the issue.