In previous article, we have seen how the middleware can be configured to serve the static files. In that article, we have seen that the static files from web root directory are by default picked up by this middleware.
What if the application is required to serve file from any other directory inside content root (i.e. directory which is sibling to web root) ?
Sometimes, the static files (especially images) might also be kept on a different servers and web application might be required to access them via a shared path. How to achieve this in .NET core web applications ?
In this article, let’s see how the static files middleware can be configured to meet the scenarios mentioned above.
Serve Files from Content Root
For demo, let’s create an ASP .NET Core web application (MVC) using Visual Studio. Then create a folder, Images, inside the project. Add an image to the directory.
Now, let’s see how this image can be shown on UI.
Below code snippet shows how these options can be passed. These options has two settings:
- File Provider – a file provider that will map to some path where the files should be present. This example uses PhysicalFileProvider and it maps to Images folder inside Content Root directory.
- Request Path – relative path from URL. When this token is found in URL, a file from Images folder would be served.
If the files from web root and files from any other folder are required to be served, then static files middleware can be configured two times as shown in below example.
Serve Files from Network Path
This is an interesting case. Let’s say the images folder is somewhere on shared network path.
It might be because the images are being shared by more than one application or it might be because the images are getting downloaded from third party providers by some background jobs or there might be any other reason for having images at some shared path.
There are multiple ways to serve the files from shared path.
Option 1 – Static Web App
First option is via exposing a static web app and then the requests from web application can be redirected to the static web app hosted (via URL rewriting maybe). This is probably simplest option.
Options 2 – Modify Rights For Web App’s Identity
But if the static files from network drive are not required to be exposed as a static web site, then the other option is – .NET core web application should access those files using File Provider. But there might be a problem.
What if the .NET core application’s identity does not have access to the network path ? Well, I would prefer to provide rights to application’s identity so that application does not know any credentials about the shared path.
Option 3 – Network Credentials
But in case, changing rights for web application’s identity is not possible, then below code example shows how the credentials can be used to configure static files middleware.
Credit: I got to know about this solution from here.
Again, I would still advice to not keep credentials with application. So if you still need to use network credentials as shown in above code snippet, I would suggest to keep those credentials at some secured location (like key vault).
Have you used PhysicalFileProvider with static files middleware ? Let me know your thoughts.