Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UseStaticFiles in ASP.NET Core 1.0

I can't find many proper Core 1.0 tutorials yet, but when I google the method name, I get umpteen examples that say to include in Startup.cs:

app.UseDefaultFiles();
app.UseStaticFiles();

Yet I get compile errors that neither method exists on app, which is type IApplicationBuilder. Are these calls no longer required, or named totally different, or set somewhere else?

like image 433
ProfK Avatar asked May 14 '16 13:05

ProfK


People also ask

What is app UseStaticFiles () in .NET Core?

UseStaticFiles() method adds StaticFiles middleware into the request pipeline. The UseStaticFiles is an extension method included in the StaticFiles middleware so that we can easily configure it. Now, open the browser and send http request http://localhost:<port>/default.html which will display default.

How do I serve a static file in NET Core?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).

Where do I put static files in NET Core?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root.

How can add wwwroot folder in ASP.NET Core API?

In order to add the wwwroot folder, right-click on the project and then select add => new folder option and then provide the folder name as wwwroot.


1 Answers

in your project.json make sure you have a reference

"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final"

in RC2 that will likely need to change to Microsoft.AspNetCore.StaticFiles

then you should be able to use

app.UseStaticFiles();
like image 98
Joe Audette Avatar answered Sep 23 '22 08:09

Joe Audette