Detailed Description: ASP. net mvc uses the Razor engine to generate static pages, mvcrazor
Recently, I have been studying ASP. NET MVC to generate static pages. So today is a learning note!
Implementation principle and steps:
1. Find the corresponding view through ViewEngines. Engines. FindView. For some views, use ViewEngines. Engines. FindPartialView;
2. Set the Model in the context object;
3. Call the Render () method of the view to save the rendering result to a physical static file;
Using System; using System. IO; using System. text; using System. web. mvc; namespace Whir. foundation. UI {// <summary> /// description: help class for generating static pages /// </summary> public class StaticPageHelper {// <summary> /// generate static pages based on The View /// </summary> // /<param name = "htmlPath"> absolute path where the static page is stored </param> // <param name = "context"> ControllerContext </param> // <param name = "viewPath"> View name </param> // <param name = "masterName"> template View Figure name </param> /// <param name = "model"> parameter entity model </param> /// <param name = "html"> return information </param >/// <param name = "isPartial"> distribution view </param> /// <returns> returns true if the view is generated successfully, failure false </returns> public static AjaxResult GenerateStaticPage (string viewPath, string htmlPath, ControllerContext context, object model = null, bool isPartial = false, string masterName = "") {var ajaxResult = new AjaxResult (); try {// create a static page Record if (! Directory. exists (Path. getDirectoryName (htmlPath) {Directory. createDirectory (Path. getDirectoryName (htmlPath);} // delete an existing static page if (File. exists (htmlPath) {File. delete (htmlPath);} ViewEngineResult result = null; if (isPartial) {result = ViewEngines. engines. findPartialView (context, viewPath);} else {result = ViewEngines. engines. findView (context, viewPath, masterName);} if (model! = Null) {context. controller. viewData. model = model;}/** set the temporary data dictionary as a static identifier * You can use TempData ["IsStatic"] in the view to control the display of certain elements. */If (! Context. Controller. TempData. ContainsKey ("IsStatic") {context. Controller. TempData. Add ("IsStatic", true);} if (result. View! = Null) {using (var sw = new StringWriter () {var viewContext = new ViewContext (context, result. view, context. controller. viewData, context. controller. tempData, sw); result. view. render (viewContext, sw); string body = sw. toString (); File. writeAllText (htmlPath, body, Encoding. UTF8); ajaxResult. isSucess = true; ajaxResult. body = "Storage path:" + htmlPath;} else {ajaxResult. isSucess = false; ajaxResult. B Ody = "An error occurred while generating the static page! View not found! ";}} Catch (IOException ex) {ajaxResult. isSucess = false; ajaxResult. body = ex. message;} catch (Exception ex) {ajaxResult. isSucess = false; ajaxResult. body = ex. message;} return ajaxResult ;}}}
AjaxResult is a self-encapsulated class. You can also replace it with your own encapsulated class.
public class AjaxResult { public bool IsSucess { get; set; } public string Body { get; set; } }
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.