Detailed Description: ASP. net mvc uses the Razor engine to generate static pages, mvcrazor

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.