简体   繁体   English

保存动态生成的HTML页面ASP.NET

[英]Save dynamically generated HTML page ASP.NET

With ASP.NET MVC, I generated an html page 使用ASP.NET MVC,我生成了一个html页面

Example: http://example1234.com/Persons/details/15 例如: http//example1234.com/Persons/details/15

Changing the last digit changes the value of the fields which I imported with @HTML helpers 更改最后一位数字将更改我使用@HTML helpers导入的字段的值

I would like to automatically save this page somewhere to the server, to make it static. 我想将此页面自动保存到服务器的某个位置,以使其静态。

Something like PersonNr15.html with the generated content hardcoded into that page. 诸如PersonNr15.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>

What you need to do is render the view to a string, and then save the string to a file like you would any other string. 您需要做的是将视图呈现为一个字符串,然后像对待其他任何字符串一样将其保存到文件中。 The rendering of an MVC view to a string is covered in previously answered questions on here such as This question MVC视图到字符串的呈现已在此处的先前回答的问题中涵盖,例如This Question

I changed my code myself. 我自己更改了代码。 I used my self-made template, and changed the words like #NAME# After changing the variable words from the file, save the file, make a PDF out of it. 我使用了自己制作的模板,并更改了诸如#NAME#之类的词。从文件中更改了可变词之后,请保存文件,然后从中创建PDF。 and done. 并做了。 (PDF wasn't part of the question, but i added it in for those interested). (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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM