Home > OS >  Why ASP.NET Core web application does not serve static web assets when started from command line?
Why ASP.NET Core web application does not serve static web assets when started from command line?

Time:12-23

This title would be too long, but this is more isolated issue:

Why ASP.NET Core web application does not serve static web assets when started from command line and the ASPNETCORE_ENVIRONMENT is not set, or set to any other value than "Development"?

Context

I've created a new ASP.NET Core Web Application using the VS 2022 built in template (.NET 6, but I do not think the issue/question is .NET 5 specific) I did not altered the created application in any way

I am able to run my ASP.NET Core web application under IIS Express, or without IIS Express using Visual Studio, based on launchsettings.json., I mean using the green triangle dropdown, and pick WebApplication instead IIS Express


Just for the sake of curiosity, I've tried to launch the WebApplication1.exe from the bin folder. It starts, I can access it via browser using https://localhost:5001/, the page loads, but without CSS, JS. I can see that all requests for static resources have a 404 response.

I suspect that I should somehow configure the app, where the static web resources are. I see that the corresponding WebApplication1.runtimeconfig.json and WebApplication1.staticwebassets.runtime.json (formerly WebApplication1.staticwebassets.xml) are there... but later I figured out that magically the environment ASPNETCORE_ENVIRONMENT is related to the issue.

What I've tried so far?

In the command shell I set the environment variable to

SET ASPNETCORE_ENVIRONMENT=Development

...then started then I started WebApplication.exe, this case all is working, static web assets are served.

Question

In the very simple Program.cs and startup code I do not see any conditional logic regarding ASPNETCORE_ENVIRONMENT variable, so I do not understand the issue, why is the difference? (I've also checked the differences between the two appsettings.json and appsettings.Development.json: no related differences.

As an ultimate goal, I would like to automate the start of the WebApplication.exe for functional testing purposes, and I would like to start it sometimes as ASPNETCORE_ENVIRONMENT=Development but other times without that setting.

Why is the difference regarding static web assets serving depending on ASPNETCORE_ENVIRONMENT=Development, and how to run and serve static web assets without that setting from command line?

CodePudding user response:

The question is a bit misleading, as the artifacts in bin/Debug folder is for debugging and not for general testing, so it is not guaranteed to work or supposed to work as you described.

The reason why you found it is not working is because there is no static assets in bin/Debug folder. It works in development environment and not in production (the default when no environment set) because the default builder calls UseStaticWebAssets only in development environment.

https://github.com/dotnet/aspnetcore/blob/4e7d976438b0fc17f435804e801d5d68d193ec33/src/DefaultBuilder/src/WebHost.cs#L221

Inside UseStaticWebAssets, it processes .staticwebssets.runtime.json file to resolve the real path of your static assets. That is how it works and why it does only when environment is development.

If you want to have it works as it supposes to, you should publish your project to a folder and call the executable from there. The publish tool will also publish your static assets into that folder so it will work whether you set the environment config or not.

  • Related