2014-10-17 123 views
0

在ASP.NET MVC,我生成的HTML页面保存动态生成HTML页面ASP.NET

例子: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

显示一些代码。尤其是将HTML返回给浏览器的部分。你尝试了什么? – Alexander 2014-10-17 11:28:13

+0

'我想自动将此页面保存到服务器的某个位置,使其成为静态' 只想知道为什么要将页面保存为静态? – Raghuveer 2014-10-17 11:43:51

+0

@irvgk对不起,如果我使用的英文是不正确的单词“静态”,但我想将它改为PDF后,所以输出的文本应该只是在保存的HTML文件..所以,如果我打开Person15.html它会显示与人15的数据相同的文件。是否这样更好地解释? – SNT 2014-10-17 11:55:22

回答

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); 
    } 
0

你需要做的是渲染视图为一个字符串,然后将字符串保存到你这样的文件,任何其他字符串。将MVC视图呈现为字符串的方法在此处回答的问题中已有介绍,如This question