Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a working project configured for Razor pages, MVC pages, and Web API pages. All seems well with app functionality, but for some reason, my static resources in the wwwroot are not accessible.

If I include a link to static resource on a Razor or MVC page, the page runs fine, but links to static resources are broken. For example, referencing an image in the root of the wwwroot folder:

 <img src="~/icon_shuttle.png" asp-append-version />

generates an appropriate link

https://localhost:44393/icon_shuttle.png

The image, and other static resources such as scripts return 404.

I do of course have app.UseStaticFiles(); and moved it to the top of my configure pipeline just be sure.

Any thoughts? I assume this is related to enabled routing for both Razor and MVC in the same project, but those resources are all working fine, so a failure only with static files seems strange.

Thanks very much in advance.

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddControllersWithViews();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseStaticFiles();
    app.UseHttpsRedirection();           
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

I did try (unsuccessfully) to manually specify the web root in Programs.cs using webBuilder.UseWebRoot

   public class Program
    {
        public static void Main(string[] args)
        {
            WebHost.CreateDefaultBuilder(args)
              .UseUrls("http://0.0.0.0:4242")
              .UseWebRoot(".")
              //Tried .UseWebRoot(@".WebSitewwwroot") to no avail
              .UseStartup<Startup>()
              .Build()
              .Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
               .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    //added UseWebRoot here also, to no avail
                    //webBuilder.UseWebRoot(@".WebSitewwwroot");
                });
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
512 views
Welcome To Ask or Share your Answers For Others

1 Answer

Found 'an' answer, but still unsure why default UseStaticFiles() routing broke when I added MVC routing, but the fix was by modifying startup.cs code for UseStaticFiles to:

app.UseStaticFiles(new StaticFileOptions
  {
  FileProvider = new PhysicalFileProvider(env.ContentRootPath + "\wwwroot"),
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...