In this article, you are going to be introduced to the basics of the code that allows you to create dynamic content on your web pages using WebMatrix. You will soon get familiar with ASP.NET’s Razor Syntax, and start developing useful web applications in no time! The adventure starts below…
Step # 1: First of all create a new empty site. To do this, follow: 1) run WebMatrix, 2) navigate to "Quick Start" window, 3) select "Site From Template", 4) select "Empty Site", 5) name it: "BasicsSyntax" and click “OK”.
If you wish, you could also use the previously created “HelloWorld” site and add an empty page there instead. To do this, follow: 1) locate and click on the “New” option from the standard toolbar, 2) select “CSHTML” as the file type, 3) finally name the page: “BasicsSyntax” and click “OK” (please refer to images shown below).
Step # 2: Once you’ve done this – you’ll automatically be presented with the contents of the newly created page. To continue, replace the existing markup with the following (we’ll be referring to it as you go):
<!-- That's how you display the contents of a variable -->
Step # 3: When you’re finished, it’s now time to see your web page in action! Click on the “Run” option from the standard toolbar to launch the page in a browser – what you’ll see should correspond to the image shown below.
Step # 4: Let’s now take a closer look on the underlying code. As you’ve probably noticed, your page consists of the two components, namely – the HTML/CSS markup and the ASP.NET Razor Syntax (please see above).
You can embed the code in any of the three ways: as an inline expression – lines # 25 & 35 (by preceding the code with a single “@” character, notice that there is no requirement for a semicolon this time), a single statement block – line #23 (similar like before but with an opening and closing curly bracket embracing the code, notice the semicolon at the end of each statement), or a multi statement block – lines # 31-33 (similar like before but now with the code placed vertically between the opening and closing bracket, again you need a semicolon ending each statement).
As you work with code and it grows in size, it’s essential that you know how to comment certain parts so that it can remind you (or others) exactly what it does – should it be necessary. In WebMatrix, when commenting markup you can make use of both single-line (lines # 16, 23, 25, 35) and multiple-line (lines # 18-21, 27-30) comments. The same applies to code; for single-line (line # 2) and multiple-line (lines # 4-7) comments.
When working with variables, you declare them by preceding with an optional word “var” (for implicit type) – line # 23 or a predefined type (for explicit type) – line # 32 which can be one of the commonly known data types including: int, float, double, string, bool, DateTime; initialisation is as simple as using an assignment operator: “=” and a value (for numerical data) followed by a semicolon/double quotes (for textual data).
Displaying Variable’s Contents
In order to display the contents of a variable you simply precede its name with a “@” character (lines # 25, 35). You can combine the contents of several variables by placing a “+” character between them (line # 32).
Step # 5: Congratulations! You have now completed all the activities related to this article.
What have we done?
In this tutorial, you have been introduced to basics of the code that allows you to create dynamic content on your web pages using WebMatrix. In the following articles, you’ll learn more on how to use variables to store predefined data as well as data input by the user – which can then in turn be used to make certain decisions. It’s all yet to come, please stay tuned! 🙂