Exercise 3: Creating and Consuming Razor HelpersASP.NET Web Pages Helpers are components that provide certain functionality, and can be accessed by any Razor or ASP.NET Web Page by using a single line of code. You can build your own helpers, which are stored as .cshtml|vbhtml razor files, or use the built-in helpers from ASP.NET or Razor. Using helpers will increase your code reusability, organization and maintainability because you will be creating separate units for specific features. In addition, you could take advantage of built-in or third party helpers to easily interact with multimedia, social networks, security, email and data grids. In this exercise, you will learn how to consume and create Razor helpers in your solution. To do that, you will first use a WebImage helper to display images with watermarks, and then you will use a WebGrid helper to show the album data. At the end, you will create a custom helper with Razor syntax to show the current date. Task 1 – Consuming the Razor WebImage HelperIn this task, you will learn how to use Razor WebImage Helper to display an image and add a watermark on it. Note: WebImage Razor Helper provides functionality to manage images in your page.The usage of WebImage helper requires two steps:1. You need to work with Razor syntax and create a variable that will contain the image: @{ var myImage = WebImage("/Content/myImage.jpg") // Work with the image… } @Code Dim myImage = WebImage("/Content/myImage.jpg") ' Work with the image… End Code2. Then, in order to load the image in the page, you have to show the variable that contains the image inside an HTML <img/> tag: <img src="@myImage"/>Here you will find a reference of the additional WebImage Razor functions that are available: - WebImage(string Path):Loads an image from the specified path. - WebImage.AddImagesWatermark(string Path) - WebImage.AddTextWatermark(string text): Adds the specified text to the image. - WebImage.FlipHorizontal()/WebImage.FlipVertical(): Flips the image horizontally /vertically. - WebImage.GetImageFromRequest([opt]string postedFileName):Gets and image from request after being posted. - WebImage.Resize(width, height):Resizes the image to the size specified in pixels. - WebImage.RotateLeft() /WebImage.RotateRight():Rotates the image left / right. - WebImage.Save(path):Saves the image to the specified path.You can find more information about Razor WebImage Helper in
ASP.NET Web Pages with Razor Syntax Book Beta.
Task 2 – Running the ApplicationIn this task, you will run the application to verify that the image with the watermark is loaded.
Task 3 – Adding a WebGrid Helper to Browse ViewIn this task, you will add a WebGrid helper to show a grid of albums inside Browse view. Note: The WebGrid Helper renders an HTML table that displays data from the model or from a database. It supports data formatting, paging and sorting.It generates a collection of Columns and parameters within the given data source, which is rendered as an HTML table that can be accessed after the grid is generated.To use a grid from a model, you will first need to generate the grid and assign it to a variable with the WebGrid creation method, which receives the model data structure as a required parameter:C#@{ var grid = new WebGrid(Model.Elements);}Visual Basic@Code Dim grid = New WebGrid(Model.Elements)End CodeThen, to render the grid in the page you will have to use a GetHtml() method:@{ var grid = new WebGrid(Model.Elements); grid.GetHtml();}Visual Basic@Code Dim grid = new WebGrid(Model.Elements) grid.GetHtml()End CodeHere you will find method references if you want to use additional features:C# WebGrid(dynamic IEnumerable<object> source (required field), IEnumerable<string> columnNames (default is null), string defaultSort (default is null), int rowsPerPage (default it 10), bool canPage (default is true), bool canSort (default is true), string ajaxUpdateContainerId (default is null), string fieldNamePrefix (default is null), string pageFieldName (default is null), string selectionFieldName (default is null), string sortFieldName (default is null), string sortDirectionFieldName (default is null) );Visual BasicWebGrid(dynamic IEnumerable(Of Object) source (required field), IEnumerable(Of String) columnNames (TypeOf default Is Nothing), String defaultSort (TypeOf default Is Nothing), Integer rowsPerPage (default it 10), Boolean canPage (TypeOf default Is True), Boolean canSort (TypeOf default Is True), String ajaxUpdateContainerId (TypeOf default Is Nothing), String fieldNamePrefix (TypeOf default Is Nothing), String pageFieldName (TypeOf default Is Nothing), String selectionFieldName (TypeOf default Is Nothing), String sortFieldName (TypeOf default Is Nothing), String sortDirectionFieldName (TypeOf default Is Nothing))C# WebGrid.GetHtml(string tableStyle (default is null), string headerStyle (default is null), string footerStyle (default is null), string rowStyle (default is null), string alternatingRowStyle (default is null), string selectedRowStyle (default is null), bool displayHeader (default is true), bool fillEmptyRows (default is false), string emptyRowCellValue (default is null), IEnumerable<WebGridColumn> columns (default is null), IEnumerable<string> exclusions (default is null), WebGridPagerModes mode (default is 3), string firstText (default is null), string previousText (default is null), string nextText (default is null), string lastText (default is null), int numericLinksCount (default is null) );Visual BasicWebGrid.GetHtml(String tableStyle (TypeOf default Is Nothing), String headerStyle (TypeOf default Is Nothing), String footerStyle (TypeOf default Is Nothing), String rowStyle (TypeOf default Is Nothing), String alternatingRowStyle (TypeOf default Is Nothing), String selectedRowStyle (TypeOf default Is Nothing), Boolean displayHeader (TypeOf default Is True), Boolean fillEmptyRows (TypeOf default Is False), String emptyRowCellValue (TypeOf default Is Nothing), IEnumerable(Of WebGridColumn) columns (TypeOf default Is Nothing), IEnumerable(Of String) exclusions (TypeOf default Is Nothing), WebGridPagerModes mode (TypeOf default Is 3), String firstText (TypeOf default Is Nothing), String previousText (TypeOf default Is Nothing), String nextText (TypeOf default Is Nothing), String lastText (TypeOf default Is Nothing), Integer numericLinksCount (TypeOf default Is Nothing))
Task 4 – Running the Application
Task 5 – Creating a Custom Helper
Task 6 – Running the Application
Next Step |
.png)
.png)
.png)
.png)
.png)
.png)