1

I am trying to make only static files accessible which are in the wwwroot subfolder of my .NET 4.x project, following the accepted answer from Map to wwwroot in ASP.Net 4?

This means that the MVC script Scripts/LoginPage/app.js will no longer be accessible at all, while wwwroot/Scripts/LoginPage/app.js will be accessible through http://localhost:8080/Scripts/LoginPage/app.js.

However, the project has MVC pages with Bundles, and I am now trying to configure the bundles correctly. The old bundle config is like this:

bundles.Add(
    new ScriptBundle("~/Bundles/Scripts/loginpages/index").Include(
        "~/Scripts/LoginPage/application.js",
        "~/Scripts/LoginPage/index.js"));

The MVC page is rendering the bundle like this:

@Scripts.Render("~/Bundles/Scripts/loginpages/index")
  • If I just keep everything as is, the page contains the unbundled scripts as follows:

    <script src="/Scripts/LoginPage/application.js"></script>
    <script src="/Scripts/LoginPage/index.js"></script>
    

    but the files themselves are not accessible.

  • If I move the scripts but keep the bundle config unchanged, scripts are not included in the HTML at all.

  • If I move the scripts into the wwwroot subfolder, and change my bundles to reference the scripts in that subfolder:

    bundles.Add(
        new ScriptBundle("~/Bundles/Scripts/loginpages/index").Include(
            "~/wwwroot/Scripts/LoginPage/application.js",
            "~/wwwroot/Scripts/LoginPage/index.js"));
    

    then in the debug version this yields the following result HTML:

    <script src="/wwwroot/Scripts/LoginPage/application.js"></script>
    <script src="/wwwroot/Scripts/LoginPage/index.js"></script>
    

    but the content of the wwwroot folder (which includes the unbundled scripts) will be available at /, not /wwwroot/, thus the files are not found.

How can I configure my scripts/bundles correctly?

Alexander
  • 19,906
  • 19
  • 75
  • 162
  • Have you verified that `http://localhost:8080/Scripts/LoginPage/app.js` actually works? – GSerg Feb 25 '22 at 08:26
  • @GSerg I have verified that I can access it if I move the Scripts folder into wwwroot/ manually after the build. – Alexander Feb 25 '22 at 10:22
  • Then surely `` would also work? – GSerg Feb 25 '22 at 11:06
  • @GSerg As I understand it, the `` is generated by MVC (in debug build only) in the line `@Scripts.Render("~/Bundles/Scripts/loginpages/index")` based on the bundle definition's `.Include` line. So I would have to drop the whole bundling stuff and start doing everything manually. Which is the last resort. – Alexander Feb 25 '22 at 12:02
  • I mean that under your first bullet you have ` ` that you say do not work. If they don't work, then `http://localhost:8080/Scripts/LoginPage/application.js` wouldn't either. – GSerg Feb 25 '22 at 15:44
  • Under the first bullet point, the javascript file is in the disk folder `Scripts`, and I have configured the server to only serve from the disk folder `wwwroot`, so to serve from `http://localhost:8080/Scripts`, the file needs to go into `wwwroot/Scripts`, but I cannot get the Bundle mechanism to find it there. – Alexander Feb 25 '22 at 16:11

0 Answers0