ASP.NET WebMatrix Beta 2 - Getting Started

Posted by: Malcolm Sheridan , on 10/16/2010, in Category ASP.NET
Views: 64617
Abstract: The following article takes you through the process of enabling routing in your ASP.NET WebMatrix website.
Microsoft has released a new beta 2 version of WebMatrixWebMatrix is a free, lightweight set of web development tools that provides the easiest way to build websites yet. It includes IIS Developer Express and SQL Server Compact Edition to get you started quickly. In the upcoming weeks I’ll walk you through getting started with WebMatrix and you’ll see how easy it is to create dynamic websites. It is important to remember this is beta software, so compiles can be slow, there’s no intellisense and sometimes WebMatrix crashes. But isn’t that part of the fun?!
Before you begin you need to install it, WebMatrix can be installed easily through the Web Platform Installer. Once the install is finished you’re ready to begin. 

 

Open WebMatrix from your Start menu and choose Site From Template
image
WebMatrix comes with a number of default templates, but to get started choose Empty Site and name it Starter.
image_2
Once the website has loaded you’ll notice the interface is very different from Visual Studio.
image_8
In the bottom left navigation pane there are four options:
·         Site
·         Files
·         Databases
·         Reports
Site contains general information about the website, such as the URL and path. Files display all of the files and folders in your website. This is where you’ll spend the majority of your time. Databases allow you to create or connect to an existing database, create table and add data. Reports allow you to run SEO reports for your site. 
Choose Files to get started. Out of the box the website is empty. This is good because you don’t need to go through an exercise to delete unnecessary files. Choose Add a file to your site:
image_4
This example uses the Razor view engine. The Web Forms view engine is available too, but I like Razor. Choose CSHTML and call the page default.cshtml:
image_3
The default.cshtml page will be displayed. Now it’s time to start coding! Start by adding the following code to your page:
<!DOCTYPE html>
<html lang="en">
      <head>
            <meta charset="utf-8" />
            <title>Main Page</title>
      </head>
      <body>
          <h1>Hello World!</h1>
      </body>
</html>
 
Click Run from the ribbon to start debugging the page:
image_5
The website will now run and display Hello World! That’s pretty basic, so let’s add some dynamic content to the page. Because you’re using the Razor view engine, if you’ve been working with ASP.NET MVC 3 Preview 1, you’ll already have a heads up. If not I’d suggest reading my article ASP.NET MVC 3 Preview 1 – Razor Syntax. Everything in Razor begins with the @ character, so to print out the date and time, do this.
<!DOCTYPE html>
<html lang="en">
      <head>
            <meta charset="utf-8" />
            <title>Main Page</title>
      </head>
      <body>
          <h1>Hello World!</h1>
        <h2>The date and time is @DateTime.Now</h2>
      </body>
</html>
 
Here’s the result.
image_6
The @ character replaces both of the following syntax:
 
<%: %>
<% %>
 
Immediately you can see the amount of code that is being reduced. One character is replacing many. Everything in the Razor view engine is auto HTML encoded by default, so you could do this safely:
 
<h2>@("<script>alert('" + @DateTime.Now + "')</script>;")</h2> 
Running the website safely prints out the encoded HTML. If you wanted the HTML to not be auto HTML encoded, then the easiest way is to wrap it in an MvcHtmlString object. By doing this you’re letting Razor know that it’s alright to not HTML encode this expression. The above code can be rewritten like this:
<h2>@System.Web.Mvc.MvcHtmlString.Create("<script>alert('" + @DateTime.Now + "');</script>")</h2>               
If you wanted to not use the fully qualified name, add a using statement to your page:
@using System.Web.Mvc;
 
@MvcHtmlString.Create("<script>alert('" + @DateTime.Now + "')</script>;")
Running the code now will result in the following error:
image_7
This is because the System.Web.Mvc assembly isn’t referenced in the project. Adding the reference is as easy as adding a Web.config file and add the following code:
<configuration>
    <system.web>
        <compilation debug="false" targetFramework="4.0">
            <assemblies>               
                <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            </assemblies>
        </compilation>
    </system.web>
</configuration>
 
The assembly is now being referenced and you can run the website and the HTML will not be encoded. Be careful because you are responsible for the HTML that is being rendered on the page.
WebMatrix Beta 2 looks promising. This has been an introduction and I’ll be taking you through some more features in upcoming articles.
The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Mikesdotnetting on Wednesday, November 10, 2010 12:03 AM
Mal, the easiest way to add unencoded strings to Web Pages is to wrap it in an HtmlString rather than importing System.Web.Mvc and using the MvcHtmlString version:

@(new HtmlString("<script>alert('" + @DateTime.Now + "')</script>;"))

Or to create your own Helper, although it looks like the team will be adding an AsHtml helper soon.
Comment posted by Malcolm Sheridan on Wednesday, November 10, 2010 4:26 PM
@Mikesdotnetting
Thanks for that Mike.  I'm so used to MVC that's the first thing I think of!

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook