2014-10-17 122 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