I have a Blazor WASM (not hosted) project with Todo and TodoList models. I put a text file in wwwroot folder and try to read it in TodoList constructor by File.ReadAllLines() method passing the path "todos.txt".
Project structure:
public class TodoList
{
public TodoList()
{
Todos = new List<Todo>();
try
{
var lines = File.ReadAllLines("todos.txt");
foreach (string line in lines)
{
Todos.Add(new Todo { Title = line });
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
}
public List<Todo> Todos { get; set; }
}
public class Todo
{
public string Title { get; set; }
public bool Checked { get; set; } = false;
}
I create TodoList object in a razor component:
@page "/todopage"
...
@code {
private TodoList todoList = new TodoList();
...
}
Running app throws the exception: Could not find file "/todos.txt". Although I can open the file directly by request https://localhost:44361/todos.txt when the app is running.
Why can't the app find the text file? How can I open and read it?