Asp.net template engine Razor call external method usage example

Source: Internet
Author: User

Asp.net template engine Razor call external method usage example

First, use Razor to read cshtml, parse cshtml, and specify cacheName.

This step is repeated. To follow the DRY principle, encapsulate this code as a RazorHelper () method.

1

2

3

4

5

6

7

8

9

10

11

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 Use Razor to call external methods in cshtml

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

1

2

3

4

5

6

7

8

9

10

11

12

@ Using WebTest1.RazorDemo; <! -- Namespace of the classes test1 and test2 -->

<! 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 a general processing program to return the read cshtml file to the customer.

1

2

3

4

5

6

Public void ProcessRequest (HttpContext context)

{

Context. Response. ContentType = "text/html ";

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

Context. Response. Write (html );

}

Why do we need to call methods in the cshtml file?

First let's look at a tedious process. Insert checkbox in cshtml.

1. General processing program

1

2

Bool gender = true;

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

2. process the checked status of the checkbox in the cshtml File

<Input type = "checkbox" @ (Model. Gender? "Checked": "")/>
<! -- Brackets are used to change the priority. Otherwise, the compiler will process the expression after the dot Model as a string. -->

Is it messy? Virgo cannot bear it.

We know that the method can encapsulate some repeated code and call the method to make the cshtml page simpler.

For example:

Insert a checkbox on the cshtml page.

1. First encapsulate a CheckBox () method

1

2

3

4

5

6

7

8

9

10

11

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 general processing programs

 

1

2

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

Context. Response. Write (html );

3. Call the CheckBox () method in the cshtml file to insert the checkbox into the cshtml

 

1

2

3

4

5

6

7

8

9

10

11

@ Using WebTest1.RazorDemo; <! -- Namespace of the classes test1 and test2 -->

<! 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>

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.