ASP. NET MVC uses the Razor engine to generate static pages

Source: Internet
Author: User

Implementation principles and procedures:

1. Through ViewEngines.Engines.FindView find the corresponding view, if it is a partial view, then use: ViewEngines.Engines.FindPartialView;

2. Set the model in the context object;

3. Call the render () method of the view to save the render result to a physical static file;

Using the view engine for static operations, there are two main classes used: The Viewengines class and the ViewContext class. Use the Viewengines class's Findview method or Findpartialview method to find the view page that you want to make static, and then populate the data model with the ViewContext class into the view template to get the page content and generate a static page.

The ViewContext class is primarily about encapsulating information related to rendering views.

Here is a reference to the ASP. NET MVC 5 Advanced Programming Information:

    1. The view engine is a static Viewenginecollection type object that can contain all the registered view engines.
    2. The Findview method iterates the view engine registered in Viewenginecollection and invokes the Findview method on each view engine, passing in the view name as a parameter. This is how viewenginecollection asks each view engine if it can render the specified view.
    3. The Findpartialview approach works almost the same as Findview, except that it focuses on finding partial views.
/// <summary>  ///Description: Static page Generation helper class/// </summary>   Public classstaticpagehelper{/// <summary>      ///generate a static page from the View view/// </summary>      /// <param name= "Htmlpath" >absolute path where static pages are stored</param>      /// <param name= "context" >ControllerContext</param>      /// <param name= "ViewPath" >View name</param>      /// <param name= "Mastername" >Template view name</param>      /// <param name= "model" >parametric entity Model</param>      /// <param name= "html" >return information</param>      /// <param name= "ispartial" >whether the view is distributed</param>      /// <returns>build succeeded returns True, failed false</returns>       Public StaticAjaxresult Generatestaticpage (stringViewPath,stringHtmlpath,controllercontext context,ObjectModel =NULL,BOOLIspartial =false,stringMastername ="")    {        varAjaxresult =NewAjaxresult (); Try        {            //Create a static page directory            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; //find the corresponding view through ViewEngines.Engines.FindView, if it is a partial view, use: ViewEngines.Engines.FindPartialView            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 temporary data dictionary as static identity * You can use tempdata["IsStatic" on 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(varSW =NewStringWriter ()) {                    varViewContext =NewViewContext (Context,result. View,context. Controller.viewdata,context.                    Controller.tempdata, SW); //call the Render () method of the view to save the render result to a physical static fileresult.                    View.render (ViewContext, SW); stringBODY =SW.                    ToString ();                    File.writealltext (Htmlpath, body, Encoding.UTF8); Ajaxresult.issucess=true; Ajaxresult.body="Storage Path:"+Htmlpath; }            }            Else{ajaxresult.issucess=false; Ajaxresult.body="failed to generate static page! No View found! "; }        }        Catch(IOException ex) {ajaxresult.issucess=false; Ajaxresult.body=Ex.        Message; }        Catch(Exception ex) {ajaxresult.issucess=false; Ajaxresult.body=Ex.        Message; }        returnAjaxresult; }} Public classajaxresult{ Public BOOLissucess {Get;Set; }  Public stringBody {Get;Set; }}

Called, you can first execute the ajaxresult.issucess if it is true before calling:

Staticpagehelper.generatestaticpage ("/views/html/commandad/commandadlist.cshtml", Server.MapPath ("/static/mobile/home/commandlist.html"true);

ASP. NET MVC uses the Razor engine to generate static pages

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.