Shaurav Adhikari
Shaurav Adhikari

Reputation: 761

IApplicationBuilder does not contain UseStaticFiles()

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

Upvotes: 14

Views: 13603

Answers (4)

Manuel Alves
Manuel Alves

Reputation: 4013

Add nuget package "Microsoft.AspNetCore.StaticFiles"

Upvotes: 26

UmarKashmiri
UmarKashmiri

Reputation: 872

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.

Upvotes: 7

Shaurav Adhikari
Shaurav Adhikari

Reputation: 761

Finally, I solved the issue. I tried multiple different things like, getting a fresh copy from repository, deleting the .dll and recompiling, even tried switching between different versions of nuget packages nothing worked.

Finally, I created an entirely new solution and copied my files to the new solution and now the error is gone in the new solution. It is a very tedious solution but finally it worked. Hope it will help if someone gets into the same issue.

Upvotes: 0

DavidG
DavidG

Reputation: 118937

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;

Upvotes: 3

Related Questions