CodexBloom - Programming Q&A Platform

Visual Studio 2022 - implementing Razor Pages and Static Files loading optimization in Debug Mode

👀 Views: 93 💬 Answers: 1 📅 Created: 2025-06-08
asp.net-core razor-pages visual-studio debugging csharp

I've been banging my head against this for hours. After trying multiple solutions online, I still can't figure this out. I'm working with a frustrating scenario while working with a Razor Pages application in Visual Studio 2022. When I run my project in debug mode, the static files (like CSS and JS) are not being served correctly, leading to broken styles and non-functional scripts. The site works fine when I run it through IIS or use `dotnet run` from the command line. I have the following configuration in `Startup.cs`: ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/behavior"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); // This line is present app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); } ``` In my `launchSettings.json`, I have: ```json { "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "MyApp": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } } ``` Despite having `app.UseStaticFiles();`, the static files are not loading at all in debug mode, and I see 404 errors in the console for styles and scripts like `/css/site.css` and `/js/app.js`. I’ve triple-checked the file paths, and they're all correct. I've also tried cleaning and rebuilding the solution, ensuring that my static files are included in the project, and even deleted the `.vs` folder. None of these steps have resolved the scenario. Could there be a configuration setting or something I'm missing that prevents the static files from being served in debug mode? Any insights would be greatly appreciated! For context: I'm using Csharp on Windows. Am I missing something obvious? This is part of a larger API I'm building. I'd really appreciate any guidance on this.