简体   繁体   中英

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

添加nuget包“Microsoft.AspNetCore.StaticFiles”

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.

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM