Maurice Twomey
Maurice Twomey

Reputation: 157

ASP.NET Core folder in Area is not loading assets when published to Azure

In my ASP.Net Core 3.1 project (Visual Studio 2019), I created an Areas folder and added an Area called "Admin". Within Admin, I added a static folder called "assets". This contains all my CSS, JS, image files for the Admin Area:

enter image description here

In the startup.cs file, I added the following code so I can link to the static files in the assets folder:

enter image description here

I took that code from here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1#serve-files-outside-of-web-root

An example link in a view looks like this:

enter image description here

When I run the project locally and browse to /admin, all the assets (CSS, JS, images etc.) are loading and the site is displaying correctly.

However, when I publish my project to azurewebsites.net, the assets are not loading when I browse to /admin! The views are rendered but the CSS, JS and images links are not working.

I have checked the server and the assets folder and files are on the server.

Any help or suggestions for how can make this work on Azure would be much appreciated!

Edit: When I open web development tools (F12) on the page, the links appears as a 404 error: enter image description here

"/assets/css/icons.min.css" doesn't exist but why isn't it pointing to /Areas/Admin/assets/css like it does when I run locally?

Edit: Checking on Azure, it seems as if only the .json files (and folders that have .json files in them) are being published! I linked directly to one of the .json files and it browsed correctly so I need to figure out why VS isn't publishing all the files. enter image description here

Upvotes: 1

Views: 2290

Answers (1)

Yihui Sun
Yihui Sun

Reputation: 813

Path.Combine(env.ContentRootPath, "Areas/Admin/assets")),RequestPath = "/assets"

  1. This line of code means that when you request "/assets", it will be mapped to "Areas/Admin/assets".
  2. According to the picture you provided, there is no problem with the requested path.
  3. However, according to the 404 error prompt, it is usually checked whether the requested file exists.
  4. The files you mentioned earlier already exist.
    • I would also like to suggest you to check if the icons.min.css file actually exists in the Areas/Admin/assets/css directory.
  5. If you make sure that these files do exist,I suspect that the problem may be caused by folder permissions.

☑Edit

Checking on Azure, it seems as if only the .json files (and folders that have .json files in them) are being published! I linked directly to one of the .json files and it browsed correctly so I need to figure out why VS isn't publishing all the files.

  1. When ASP.NET Core Web applications are published by default, the following assets are included:
    1. Build artifacts
    2. Folders and files matching the following globbing patterns:
      • ***.config (for example, web.config)
      • ***.json (for example, appsettings.json)
      • wwwroot**
  2. You need to include the required files when you publish.

Upvotes: 1

Related Questions