Lukasz
Lukasz

Reputation: 129

Access network drive folder with content

I’m trying to build an ASP.NET Core MVC web application that would allow me to browse photos from a shared network drive on my network.

Note: I have tried to use IIS virtual directory to map network drive, but ASP.NET Core MVC uses Kestrel, which doesn’t allow me to use this approach.

Could someone please demonstrate how to configure ASP.NET Core MVC to somehow map/link network folder and to browse photos in an MVC view?

Upvotes: 0

Views: 2128

Answers (1)

Lukasz
Lukasz

Reputation: 129

To enable an ASP.NET Core MVC project to serve static files outside of wwwroot, you have to configure it by adding extra code to the following file:

Startup.cs

Option 1 – Use UseStaticFiles method

// Add MyStaticFiles static files to the request pipeline.
app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(@"C:\photos"),
    RequestPath = new PathString("/photos")
});

Option 2 – Use FileServer method

// Enable all static file middleware (serving of static files, default files, and directory browsing) for the MyStaticFiles directory.
app.UseFileServer(new FileServerOptions()
{
    FileProvider = new PhysicalFileProvider(@"C:\photos"),
    RequestPath = new PathString("/photos")
});

View

<img src="~/photos/sample.jpg" class="img-responsive"/>

Both options 1 and 2 have also been tested by me so I know its working

For more information refer to the following link where I found this information: http://dotnet.today/en/aspnet5-vnext/fundamentals/static-files.html

Upvotes: 3

Related Questions