Here I will also demonstrate how you can capture the user added value at the controller end with the help of model class property and create a text file as per user given file name in wwwroot folder and add or save the user provided value or data in the text file(.txt).
Now for this article first we will create a new asp.net core application. Here I have used .NET core 8. In this asp.net core mvc application we will create a folder in wwwroot, where we will create our text file.
Now let's create a model class file and add the below code.
namespace Project.Models
{
public class FileModel
{
public string TextFileName { get; set; }
public string Filetextdetail { get; set; }
}
}
In above model class file, I have declared two properties named as TextFileName, Filetextdetail. These properties I will use to capture the data and the file name from view to controller. Now let add a controller and add a IActionResult and annotate it with HttpGet. Here in this methos we will create object of model class file and pass it to view.
[HttpGet]
public IActionResult Index()
{
FileModel fileModel = new FileModel();
return View(fileModel);
}
Now let's create the view and add the below code in it.@model FileModel
@{
ViewData["Title"] = "Home Page";
}
@using (Html.BeginForm("Index", "Home"))
{
<div style="color: red;">@ViewBag.Message</div>
<div style="font-weight:bold;">Enter File Name</div>
<div>
@Html.TextBoxFor(m => m.TextFileName, new { @class = "form-control" })
</div>
<div style="font-weight:bold;">Enter Your Text</div>
<div>
@Html.TextAreaFor(m => m.Filetextdetail, new { @class = "form-control" })
</div>
<div><input type="submit" value="Add To Text File" /></div>
}
Here in above view code, I have added the reference of the model class file named as FileModel. Here one thing we need to check in asp.net core 8 mvc and above version for adding the model class reference we just need to user model class file name and with @model. [HttpPost]
public IActionResult Index(FileModel fileModel)
{
string parentPath = @"\wwwroot\DemoFiles\";
string finalPath = string.Format("{0}{1}{2}{3}",Directory.GetCurrentDirectory(), parentPath, fileModel.TextFileName , ".txt");
if (!Path.Exists(finalPath))
{
//create file
StreamWriter streamWriter = new StreamWriter(finalPath);
//Write data to file
streamWriter.WriteLine(fileModel.Filetextdetail);
//Close the instance
streamWriter.Close();
ViewBag.Message = "File created successfully.....";
}
else
{
ViewBag.Message = "File already exists....";
}
return View(fileModel);
}
The above code represents the HttpPost IActionResult method. Which will execute when user click on Submit button. Now here I have taken a variable to store the folder location where we want to create the text file. string finalPath = string.Format("{0}{1}{2}{3}",Directory.GetCurrentDirectory(), parentPath, fileModel.TextFileName , ".txt");
//create file
StreamWriter streamWriter = new StreamWriter(finalPath);
//Write data to file
streamWriter.WriteLine(fileModel.Filetextdetail);
//Close the instance
streamWriter.Close();
ViewBag.Message = "File created successfully.....";
Now click on button control our break point will hit. Here we can see the detail added by user we are getting in the model property.