3.MVC Framework Development (Razor inline function)

Source: Internet
Author: User

1. Create a razor inline function with no return value (but can output content directly)

Must start with an @ sign, no return value, but can output content directly, such as:

@helper  showtitle (string  strtitle) {    if8) {        @ ( Strtitle.substring (0,8) +"... " // output content    } Else {        @strTitle     // output     }}

2. Create a razor inline function with a return value

Must start with @functions and inside is a static method, such as outputting a picture through the book ISBN number:

@functions { Public Static stringShowimg (stringISBN) {     stringFilePath ="~/content/images/bookcovers/"+isbn+". jpg";//virtual path     stringPhysicpath = HttpContext.Current.Server.MapPath (FilePath);//To convert a virtual path to a physical path     if(File.exists (Physicpath)) {returnFilePath; }     Else{         return "~/content/images/book_01.gif"; } }}

3. Create a global razor inline function

1) Create a new App_Code folder in the project (the file will eventually compile to generate a class that can be called anywhere, without introducing a namespace)

2) Add a view file with the suffix named cshtml under the folder, such as Create razorhelper.cshtml file, then eventually compile into Razorhelper class

3) Write inline functions that have no return value or have a return value, in the same notation as in 1 and 2

4) Call the inline function method on the other page: the @ class name. Method name (parameter value), such as:

@RazorHelper. Showtitle (T.title)// no return value function @RazorHelper. SHOWIMG (item. ISBN)// function with return value

The code from the Mvcbookshop project, in the F:\Program\MvcBookShop\MvcBookShop directory of the index page, you need to leave a message

Please excuse me if you are wrong

3.MVC Framework Development (Razor inline function)

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.