Couple things.
In ASP.NET Core, you'll want to retrieve the path that the app is running on from the IWebHostEnvironment interface. In the code below, you'll see in the constructor it's using Dependency Injection to get access to that.
It has two properties,
ContentRootPath is the root of the application
WebRootPath is the wwwroot folder
Also to simplify things I've refactored writing to the file with File.WriteAllText() which is a newer convenience wrapper around StreamWriter and does exactly what is shown in the answer.
Last thing that is personal preference I've opted for string interpolation $"" instead of concatenating with the +.
public class FileSystemFileController : Controller
{
private readonly IWebHostEnvironment webHostEnvironment;
public FileSystemFileController(IWebHostEnvironment webHostEnvironment)
{
this.webHostEnvironment = webHostEnvironment;
}
public IActionResult Index(string name)
{
string path = Path.Combine(webHostEnvironment.ContentRootPath, $"data/{name}.txt");
System.IO.File.WriteAllText(path, "Write something on file");
return View();
}
}