使用ASP.NET MVC,我生成了一个html页面
示例:http://example1234.com/Persons/details/15
更改最后一位数会更改我使用@HTML
助手
我想将此页面自动保存到服务器,以使其静止。
像PersonNr15.html
这样的内容,生成的内容被硬编码到该页面中。
@model MvcApplication3.Models.Person
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
<legend>Person</legend>
<p>@Html.DisplayFor(model => model.FirstName)</p>
<p>@Html.DisplayFor(model => model.LastName)</p>
</fieldset>
答案 0 :(得分:0)
您需要做的是将视图呈现为字符串,然后将字符串保存到文件中,就像将任何其他字符串一样。将MVC视图呈现为字符串将在此处回答的问题中进行介绍,例如This question
答案 1 :(得分:0)
我自己更改了代码。 我使用了自制的模板,并更改了#NAME#这样的字样 更改文件中的变量字后,保存文件,从中制作PDF。并做了。 (PDF不是问题的一部分,但我为那些感兴趣的人添加了它。)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Person person)
datadir = ConfigurationManager.AppSettings["datadir"];
//datadirectory defined in Web.config
//also possible to hardcode it here, example: "c:/windows/PDFfolder"
wkhtmltopdf = ConfigurationManager.AppSettings["wkhtmltopdf"];
//directory to the file "wkhtmltopdf", downloaded it somewhere
//just like above, defined at web.config possible to hardcode it in
ViewData["IsModelValid"] = ModelState.IsValid ? "true" : "false";
//valid checker
if (ModelState.IsValid) //check if valid
{
db.People.Add(person); //add to db
db.SaveChanges();
var fileContents1 = System.IO.File.ReadAllText(datadir + "Template.html");
//get template from datadirectory
fileContents1 = fileContents1.Replace("#NAME#", person.Name);
//replace '#NAME#' by the name from the database table person.Name
System.IO.File.WriteAllText(datadir + "tmp\\Template." + person.ID + ".html", fileContents1);
//create a new html page with the replaced text
//name of the file equals the ID of the person
var pdf1 = new ProcessStartInfo(wkhtmltopdf); //start process wkhtmltopdf
pdf1.CreateNoWindow = true; //don't create a window
pdf1.UseShellExecute = false; //don't use a shell
pdf1.WorkingDirectory = datadir + "tmp\\"; //where to create the pdf
pdf1.Arguments = "-q -n --disable-smart-shrinking Overeenkomst." + person.ID + ".html Overeenkomst." + person.ID + ".pdf";
//get the html to convert and make a pdf with the same name in the same directory
}
return View(person);
}