Chapter 8: WebPages Files
Web Pages – Files (exactly the section titled “WebPages Files” in the W3Schools WP Tutorial menu). This is where we move from displaying static or form-based content to actually reading data from files on the server — a super useful skill when you want to store simple data without jumping straight to a full database.
We’re still in the “no-database zone” — using flat text files (like .txt or .csv) to hold information. This was very common in small sites back in the day (and still useful for prototypes, logs, or tiny data sets today).
I’ll explain it slowly, like we’re sitting together debugging in WebMatrix or VS Code, with real examples, step-by-step breakdowns, and important security notes. Ready? Let’s go! 🚀
1. What Does “WebPages Files” Mean Here?
This lesson teaches you how to:
- Read the contents of text files stored on the server
- Display that data nicely on a web page
- Parse simple structured data (especially comma-separated values — CSV style)
It focuses almost entirely on reading (not writing — writing comes in later concepts or with System.IO methods). The goal: show how a web page can pull data from a file and show it dynamically, without needing SQL Server or Access.
Key files used: Plain text files (.txt, .csv, sometimes .xml)
Where to store them? → Inside the special App_Data folder (we talked about this in the Folders lesson — remember?)
2. Why App_Data Folder? (Security First – Teacher Emphasis!)
- App_Data is a protected folder in ASP.NET
- Files here cannot be downloaded directly by typing a URL like http://yoursite.com/App_Data/mydata.txt → you get 404 or forbidden error
- Only your server-side code (in .cshtml pages) can access them
- Perfect for data files you don’t want users to see raw
Create it manually in root if it doesn’t exist:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
/ ├── App_Data/ │ └── Persons.txt ← our data file ├── Shared/ │ └── _Layout.cshtml ├── Index.cshtml |
3. Core Tools You Need (All Built-in)
| Tool/Method | What it does | Why important? |
|---|---|---|
| Server.MapPath(“path”) | Turns virtual path (~/App_Data/file.txt) into real server path (C:…\file.txt) | Paths change between dev & production — this makes code portable |
| File.ReadAllLines(path) | Reads entire file → returns string[] (array of lines) | Simple & fast for small/medium files |
| string.Split(‘,’) | Splits one line into array using comma (or any delimiter) | Turns “George,Lucas” into [“George”, “Lucas”] |
| foreach loops | Iterate over lines → then over fields in each line | Displays data row-by-row, field-by-field |
All from System.IO namespace — automatically available in Web Pages.
4. Classic Example – Reading & Displaying Names from Persons.txt
Step 1: Create the data file App_Data/Persons.txt
|
0 1 2 3 4 5 6 7 8 9 |
George,Lucas Steven,Spielberg Alfred,Hitchcock Quentin,Tarantino |
(Each line = “FirstName,LastName” — simple CSV without headers)
Step 2: The .cshtml page — ReadFile.cshtml
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
@{ // Step 1: Get real physical path to the file var dataFile = Server.MapPath("~/App_Data/Persons.txt"); // Step 2: Read all lines into array string[] lines = File.ReadAllLines(dataFile); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Reading Data from File</title> </head> <body> <h1>Famous Directors from File</h1> @if (lines.Length == 0) { <p>No data found in file.</p> } else { <table border="1" cellpadding="5"> <tr> <th>First Name</th> <th>Last Name</th> </tr> @foreach (string line in lines) { // Split line into fields string[] fields = line.Split(','); // Make sure we have at least 2 fields to avoid errors if (fields.Length >= 2) { <tr> <td>@fields[0].Trim()</td> <td>@fields[1].Trim()</td> </tr> } } </table> } <p>Total directors: @lines.Length</p> </body> </html> |
What happens step-by-step?
- Server.MapPath(“~/App_Data/Persons.txt”) → finds the real file path on disk
- File.ReadAllLines(…) → loads whole file into string[] lines
- lines[0] = “George,Lucas”
- lines[1] = “Steven,Spielberg” etc.
- Outer @foreach → loops over each line (each person)
- line.Split(‘,’) → breaks “George,Lucas” into [“George”, “Lucas”]
- Inner logic → outputs each field in a table cell
- .Trim() → removes any extra spaces
- Browser shows nice table — data came from file!
5. Another Real-World Example – Reading CSV Sales Data
App_Data/Sales.csv
|
0 1 2 3 4 5 6 7 8 9 |
Product,Price,Date Sold Laptop,₹89,999,2025-12-01 Phone,₹45,500,2025-12-02 Tablet,₹32,000,2025-12-03 |
DisplaySales.cshtml
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
@{ var filePath = Server.MapPath("~/App_Data/Sales.csv"); string[] rows = File.ReadAllLines(filePath); // Skip header row if you want (rows[0] is header) var dataRows = rows.Skip(1).ToArray(); // using LINQ - optional } <h1>Recent Sales</h1> <table border="1"> <tr> <th>Product</th> <th>Price</th> <th>Date</th> </tr> @foreach (var row in rows.Skip(1)) { // skip header var columns = row.Split(','); if (columns.Length >= 3) { <tr> <td>@columns[0].Trim()</td> <td>@columns[1].Trim()</td> <td>@columns[2].Trim()</td> </tr> } } </table> |
→ Same idea — parse CSV rows → show in HTML table
6. Important Teacher Warnings & Best Practices (2026)
- File size: ReadAllLines loads everything into memory → fine for < few MB. For huge files → use StreamReader line-by-line (advanced)
- Error handling (not in basic tutorial, but you should add!):
|
0 1 2 3 4 5 6 7 8 9 |
if (!File.Exists(dataFile)) { <p style="color:red;">File not found: @dataFile</p> return; } |
- Delimiter issues: If data contains commas (e.g. addresses) → real CSV needs quotes & proper parser (not just Split). For learning → keep simple.
- No writing here: Tutorial only shows read. To write/append:
|
0 1 2 3 4 5 6 7 |
File.AppendAllText(dataFile, "\nNewLine,Value"); File.WriteAllText(dataFile, "Overwrite everything"); |
(Use with care — backups!)
- Modern note: In ASP.NET Core → same System.IO.File, but prefer IWebHostEnvironment.ContentRootPath instead of Server.MapPath
Summary – Closing the Class
ASP.NET Web Pages – Files lesson shows:
- Store simple data in text/CSV files in App_Data
- Use Server.MapPath + File.ReadAllLines to read safely
- Parse lines with Split(‘,’) → display in loops/tables
- Great stepping stone before real databases (next lesson!)
This was how many small sites stored visitor logs, configs, or product lists before SQL became easy.
Questions for next class?
- Want to see writing to file (guestbook example)?
- Error handling + try-catch?
- Reading .xml or JSON instead?
- Or jump straight to WebPages Databases?
Just tell me — you’re doing fantastic, Webliance! Keep building from Hyderabad! 🇮🇳🚀
