Azure应用web.config部署位置及JSON文件404问题排查
Hey there, let's work through your Azure app's 404 error when trying to read local .json files. Based on what you've shared, here's a breakdown of potential issues and fixes:
First off, placing web.config in the project root is correct (it should live alongside your main entry files like index.html or Program.cs). The real issue might be an incomplete or incorrect MIME type setup in the file. A valid configuration for .json files looks like this:
<configuration> <system.webServer> <staticContent> <mimeMap fileExtension=".json" mimeType="application/json" /> </staticContent> </system.webServer> </configuration>
If your truncated config doesn't include this exact structure, that's likely why it's not working. Also, note that for ASP.NET Core apps, web.config acts as a bridge for IIS settings—you can still use this MIME map, or alternatively configure static file types directly in code (more on that below).
It's super easy to have a file locally that doesn't make it to Azure. Here's how to check:
- If using Visual Studio: Right-click your .json file, select Properties, and set Copy to Output Directory to "Copy always" or "Copy if newer".
- If using Git-based deployment (like GitHub Actions or Azure DevOps): Check your
.gitignorefile to make sure it's not excluding .json files, and verify your deployment script includes all necessary static assets.
ASP.NET Core doesn't serve static files by default. You need to add this middleware to your Program.cs before the final app.Run() call:
app.UseStaticFiles();
This tells the app to allow access to static assets like .json, .css, or image files.
Head to the Azure Portal, navigate to your App Service, then go to Configuration > General settings. Make sure the "Static files" toggle is turned on—this ensures IIS (which runs behind Azure App Service) allows serving of static content.
Sometimes the problem isn't configuration, but a wrong file path. Azure's working directory might differ from your local machine, so avoid hardcoding relative paths. Instead, build an absolute path like this:
var jsonPath = Path.Combine(Directory.GetCurrentDirectory(), "your-file.json");
This ensures your app looks in the correct directory regardless of where it's hosted.
To wrap up: If your web.config has the correct MIME map and is in the right place, the issue is almost certainly one of deployment, missing static file middleware, or an incorrect file path. Work through these checks one by one, and you should get that .json file loading properly.
内容的提问来源于stack exchange,提问作者Reponic

