Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

asp.net core - Routing in Razor/MVC enabled app can't see static resources

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");
                });
    }

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

1 Reply

0 votes
by (71.8m points)

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"),
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...