using System.IO;
using System.Security;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Windows.Forms;
using System.Xml.Linq;
using System;
@using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
public class HomeController : Controller
{
IConfiguration Configuration { get; }
public HomeController(IConfiguration configuration) => Configuration = configuration;
public IActionResult Index() => View();
// Here you can specify the path of the html file and a text for it in this case will be used to grab the value of an input text area named 'Text'. But it works as well if you want to grab an input from a button and save it as another one in a new html file or somewhere else if you want to do something like that
[HttpPost]
public IActionResult Index(string path, string text)
{
try
{
var files = Directory.GetFiles(path);
}
catch (DirectoryNotFoundException)
{
return View("Error");
}
catch (UnauthorizedAccessException)
{
return View("Error");
}
catch (ArgumentNullException)
{
return View("Error");
}
catch (ArgumentException)
{
return View("Error");
}
catch (PathTooLongException)
{
return View("Error");
}
catch (NotSupportedException)
{
return View("Error");
}
catch (SecurityException)
{
return View("Error");
}
// Here you can get the value of your textarea and save it for further use
var input = Request.Form["Text"]; // This is an example
foreach (var file in files)
{ // Here you can loop through all the files, edit them or delete them
if (file.Contains(".cshtml"))
{ // You can just look for a specific file extension here if you want or loop through all the files in your directory, edit or delete any file
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(File.ReadAllText(file));
var formElement = htmlDocument.GetElementbyId("example-form").OuterHtml;
formElement = Regex.Replace(formElement,
$"<input name=\"Text\" type=\"text\" value=\"\s*?.*?\" />",
$"<input name=\"Text\" type=\"text\" value=\"{input}\" />");
var streamWriter = new StreamWriter(file); // Replace the content of the file with a string created on the previous line
streamWriter.WriteLineAsync(formElement);
}
}
return View("Index"); // Return your view to be called after saving or editing a file or not depending on what you are trying to do.
}
}
2条答案
按热度按时间qkf9rpyu1#
字符串
你可以把它放在一个JavaScript函数中,然后在onlcick(像一个提交按钮)和/或在文本区域作为OnTextChanged调用。
pjngdqdw2#
您必须提供文本区域的ID或使用Html帮助器方法来获取它。下面是一个工作示例:
字符串