asp. net Template Engine Razor Call External Method Usage Instance

  • 2021-07-01 07:05:20
  • OfStack

This article illustrates the use of asp. net template engine Razor to invoke external methods. Share it for your reference. The details are as follows:

The first step to use Razor: Read cshtml, parse cshtml and specify cacheName.

This step is repeated, and in order to follow the DRY principle, this code is encapsulated as an RazorHelper () method


public class RazorHelper
{
  public static string ParseRazor(HttpContext context, string csHtmlVirtualPath, object model)
  {
   string fullPath = context.Server.MapPath(csHtmlVirtualPath);
   string cshtml = File.ReadAllText(fullPath);
   string cacheName = fullPath + File.GetLastWriteTime(fullPath);
   string html = Razor.Parse(cshtml,model,cacheName);
   return html;
  }
}

How to call external methods with Razor in cshtml

1. First reference the namespace of the class test1 and test2 in the cshtml file


@using WebTest1.RazorDemo;<!--test1 And test2 Namespace of the class in which -->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title></title>
</head>
<body>
 @RazorTest.test1()<br />
 @RazorTest.test2()
</body>
</html>

2. Call RazorHelper. ParseRazor () in the 1-class handler and return the read cshtml file to the customer


public void ProcessRequest(HttpContext context)
{
 context.Response.ContentType = "text/html";
 string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", null);
 context.Response.Write(html); 
}

Why call a method in an cshtml file?

First, look at a tedious process of inserting checkbox into cshtml

1. 1 generic handler


bool gender = true;
string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", new { Gender = gender });

2. checked state for processing checkbox in cshtml file

< input type="checkbox" @(Model.Gender?"checked":"") / >
< ! --Add parentheses to change the priority, otherwise the compiler will treat the expression after the point Model as a string-- >

Is it messy? Virgo can't bear it.

We know that methods can encapsulate some duplicate code, and calling methods makes the cshtml page simpler.

For example:

To insert an checkbox into the cshtml page.

1. First encapsulate an CheckBox () method


public static RawString CheckBox(string name, string id, bool isChecked)
{
 StringBuilder sb = new StringBuilder();
 sb.Append("<input type='checkbox' id='").Append(id).Append("' ").Append("name='").Append(name).Append("' ");
 if (isChecked)
 {
 sb.Append("checked");
 }
 sb.Append("/>");
 return new RawString(sb.ToString());
}

2. Read and parse cshtml files in a generic handler


string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", null);
context.Response.Write(html);

3. Insert checkbox into cshtml by calling the CheckBox () method in the cshtml file


@using WebTest1.RazorDemo;<!--test1 And test2 Namespace of the class in which -->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title></title>
</head>
<body>
 @RazorTest.CheckBox("apple","apple",true)
</body>
</html>

I hope this article is helpful to everyone's asp. net programming.


Related articles: