ASP.NET Core MVC : make file available on server

Viewed 28

I'm new to .NET Core and I'm working on an ASP.NET Core MVC app.

I'm trying to make available a file that was generated from backend. The physical path is (root) Cards/id/index.html. My problem is that when I execute my web app, I cannot browse that file.

For example:

localhost:Card/id/index.html 

I know that i have to add something to my middleware but I can't fix it.

Here is my startup.cs code

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseStaticFiles();
    app.UseStaticFiles(new StaticFileOptions()
                           {
                               FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"cards")),
                               RequestPath = new PathString("/personal-cards")
                           });

     app.UseRouting();    

     app.UseEndpoints(x => {
            x.MapRazorPages();
            x.MapControllerRoute(name:"Default",
                                 pattern: "{Controller}/{action}/{id?}",
                                 defaults: new {controller = "Home", Action="Index"});
     });
}

Can you help me?

1 Answers

You don't need write below code.

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"cards")),
    RequestPath = new PathString("/personal-cards")
});

You just need to put your cards folder under the wwwroot folder. Then your can access the file directly, you just need to make sure the url is correct.

Related