Serve Static Files Outside wwwroot in ASP.NET Core – Session9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is amin and this is session9 of asb.net core tutorial uh in this session we are going to talk about serving static files outside of www root folder if you remember in session 8 we [Music] talked about the static file and we mentioned that a static file should be located inside www root folder and in session 8 we mentioned this topic if you haven't watched session 8 you can watch it from here [Music] in this session we explain how you can serve static files and how you can create the www root folder and now we try to serve static files outside of ww root folder let's continue we can serve static files with a user static file middleware and also use server files first we try to show you how you can put your static files out of ww root folder for this purpose if you use use static files middleware you should create one static file option and inside of that static file option you should create one file provider and true path dot combine uh you can get the is the folder that you created for your static file and with environment with environment variables and content root folder and name of your folder inside your [Music] project root folder you can get the path and also then you send the request path to static file options request path it's the path that later you can colleagues for serving static files and then after that you send this static file option to your middleware let's flip to visual studio and see the process in mid in visual studio this is the project that i've been working throughout this tutorial and these are the codes that i have added during previous session let me first comment the code related to use file server middleware and uncomment the code which is related to use static file middleware for this purpose i need add one another folder uh to root of my project for [Music] being container of a static file i name it sta tick files and i will add one another folder inside this one with name of images and let me add one image to this folder i add image 2 to this folder but first before modifying the code let me check whether these file inside this static files will serve by sv.net core or not let me run my project and as you can see this is the startup uh the startup folder the startup html that i introduced to projects during previous session and let me check whether inside a static files images i have image 2.jpg and let me request for this file i receive 404 error because this folder is not recognized by asp.net and my project before adding a file option uh static file options uh i need two namespace to be added for this change first one is using microsoft dot extension that file provider because i need the file provide file provide providers during this session and also using system.io because i need to find a root path of this project and i add one static file option i will cut it a static file option new static file option let me implement required code i need one file provider and which is physical file provider it has two overloaded i use the first one which needs the root path and i will get the root path with path which is inside of system.io namespace combine and i will use environment variable and content root path also i need to send a name a folder that i have added i will copy it here and paste it here and then i add request path request path is the path that you will add in your browser to access this folder in it can be different with this folder name or it can be the same i use the same name and in my next example i will change the name and then i pass this static file to my middleware let me run my project i receive one error and because i forget to add slash here and now i rerun my project and project is up and running but i don't have access to ww root because add another folder and it is overrided to ww roots folder let me for example try to access one image in ww root and again i receive 404 but if i try to access static files images and image to that jpg you can find it is working and now i use another folder for my static file uh if you want to use both a static file container you should call app dot use a static file twice and let me the code which is related to www root folder move it after this one it is related to default startup file and now i have two aesthetic one for this uh static file container and another one for this one let me run my project again and try both container is available now or not as you can see my html.html file which is located the inside wwe root is available now and i have access to images and images one and also i have access to static files images and image too this is the code that you should add for for using another folder to be container of your static file also you can do the same process with use file server if you use a use file server for serving your static wireless middleware you can add this piece of code to your project instead of the previous code that i have added let me back to visual studio and for saving time i copy this code and paste it in my project okay let me now comment the code that i have added and uncomment the file server piece of code and for having both wwe root and a static file i will add the code that i show you in presentation it is exactly uh same as a code that i have added for a static file but this is the same name i will add one here and app dot use file server and edit file option one to the project this time i use my static file instead of the exact folder name to show you you can change the path shouldn't be the same as the folder name let me run the project ww root file and folders uh are available and also if i want to have access to static files i should go to the pad my static files slash images slash image 2 and as you can see it is working perfectly text version of this video is available in our website which its link is available in description of this video and also you have access to a link of playlist of this video series and or social network thank you for watching
Info
Channel: Golden Courses
Views: 340
Rating: undefined out of 5
Keywords: ASP.NET Core Tutorial, ASP.NET Core Static Files, ASP.NET Core Training, ASP.NET 5.0, CSharp, MVC
Id: oH5ozH52AcI
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Sat Apr 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.