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)