How to Read wwwroot Text Files Data in Asp.Net Core 8 MVC Using C#.Net

How to read / extract to get data or content of text file from wwwroot folder in controller and display on view in asp.net core 8 mvc using C#.Net.
In today's article I will show you how you can read or get the text file data present in  wwwroot folder in asp.net core 8 mvc using c#.net. So, for this article first we will create a new asp.net core mvc application and add a text file with some demo data in your wwwroot folder. 

wwwroot demo textfile

Now let's check the demo file output. 

wwwroot Demo Text File Content in Visual Studio 2022

Now add a controller and add an IActionResult HttpGet method. In this method we will add a controller and add the below code in the View.
@{
    ViewData["Title"] = "Home Page";
}
<h1>File Data In wwwroot Folder</h1><br />
<span>@ViewBag.FileData</span>
In above code I have user ViewBag to display the text file content at user end. Now let's check the controller code.  Here I have demonstrate how we can read the file on page load. 
[HttpGet]
public IActionResult Index()
{
    string folderPath = @"\wwwroot\DemoFiles\";
    string filename = "DemoTextFile.txt";
    string finalPath = string.Format("{0}{1}{2}",Directory.GetCurrentDirectory(), folderPath, filename);
if (Path.Exists(finalPath)) { var textFileData = System.IO.File.ReadAllText(finalPath); ViewBag.FileData = textFileData; } else { ViewBag.Message = "Given file does not exists."; } return View(); }
In above code i have taken two variables first to define or store the folder path available in wwwroot folder and second one is the text file name. 
string finalPath = string.Format("{0}{1}{2}",Directory.GetCurrentDirectory(), folderPath, filename);
Please check the above piece of code. In this I user string.Format to Concatenate the file and folder path. In this Directory.GetCurrentDirectory() is used for getting the current physical path of the file and then combining with the file and folder. After that I have validated the path exists or not if it exists it will read the file content. Let's discuss the piece of code for reading the content.
        var textFileData = System.IO.File.ReadAllText(finalPath);
        ViewBag.FileData = textFileData;
In above code I have used System.IO.File.ReadAllText(finalPath) to rea the file content.  Here if we dont user System.IO we will get the error. So, it always use it as given in the code. 
Error! ControllerBase.File(byte[], string)' is a method, which is not valid in the given context
 After reading the date i have stored into ViewBag. This we will display in view. Now let's put a break point and check the output.

wwwroot text file content in controller

In above code we are able to get the text file content. Now let's press F5 to check the output.

wwwroot folder text file data

Exception Handling Tip: Now let's discuss if we don't pass the correct file path what will happen. We will get the below exception. So, it always better to validate the path before reading the file content or use try catch block.
Exception while file reading in asp.net core

Post a Comment