Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IApplicationBuilder does not contain UseStaticFiles()

Tags:

asp.net-core

I tried different versions of StaticFiles.But it shows error because IApplicationBuilder does not contain UseStaticFiles().

"Microsoft.AspNetCore.StaticFiles": "1.1.1"

I am using the exact code provided in the .net documentation by Microsoft. Here is what I have in Startup.cs. Note: app is an IApplicationBuilder

app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(
                    PathString.Combine(Directory.GetCurrentDiriectory(), @"Files")),
                RequestPath = new PathString("/Files")
            });

To put it in context, I want to read the directories and files inside the "Files" folder. Here is the line of code I am using to read the content of the folder but it always returns null. Note: "initialFilePath" is relative path to the folder "File".

var contents = _fileProvider.GetDirectoryContents(initalFilePath);

Thanks

like image 473
Virodh Avatar asked Mar 24 '17 20:03

Virodh


3 Answers

Add nuget package "Microsoft.AspNetCore.StaticFiles"

like image 68
Manuel Alves Avatar answered Nov 11 '22 12:11

Manuel Alves


Open your csproj files in notepad and make sure you have the following line.

<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />

If not, add it and reload the project and rebuild.

like image 42
UmarKashmiri Avatar answered Nov 11 '22 12:11

UmarKashmiri


You are almost certainly missing a using statement. The extension method UseStaticFiles is in the Microsoft.AspNetCore.Builder namespace. just add this to the top of your Startup.cs:

using Microsoft.AspNetCore.Builder;
like image 3
DavidG Avatar answered Nov 11 '22 10:11

DavidG