ASP.NET server code can be added to your pages using the Razor syntax, based on .NET.
Razor Syntax Reference [Using C#]
TypeExampleStatement block@{ var myMessage = “Hello World”; }Inline expression<p>The value of myMessage is: @myMessage</p>Adding text to code@if(condition){<text> normal text</text>}Expression and text@myMessage, @nameServer Side Comment@* Comment *@Escape @symbol@@myMessage
Helpers
A helper is a component that simplifies common tasks, such as Twitter and Facebook integration. A number of helpers are available. These can be installed through the ASP.NET Web Pages Administration tool in the site workspace. This brings you to a package manager containing a list of available add-on packages.
You can install helpers by going to the administration page of your site http://localhost:<port>/admin and following the instructions through the Package Manager.
Adding Search Using the Bing Helper
The following code snippets show how to use the Bing Helper to add search to your site.
Set up the site to search:
@{
Bing.SiteUrl = “www.dzone.com “;
Bing.SiteTitle = “DZone Custom Search”;
}
Display a simple search box that will open results in a new browser window:
@Bing.SearchBox()
Display an search box that will show results in the same window:
@Bing.AdvancedSearchBox(
boxWidth: “300px”,
resultWidth: 600,
resultHeight: 900,
themeColor: “Green”,
locale: “en-US”)
Social Networking
The Twitter helper allows you to embed Twitter feeds on your webpage.
Display a list of tweets from your profile use :
Twitter.Profile(“username”)
Display a Twitter search area:
@Twitter.Search(“<Insert search criteria here>”)
The Facebook helper provides the capability to add a Like button to your website.
Display a Like button for the current page:
@Facebook.LikeButton()
Display a Like button for a specific page:
@Facebook.LikeButton(
href: “http://refcardz.dzone.com”,
action: “recommend”,
width: 250,
buttonLayout: “button_count”,
showFaces: true,
colorScheme: “dark”)
Adding Email
The WebMail helper makes it easy to send emails from your website.
Email information is set through a number of variables.
@{
WebMail.SmtpServer = “your-SMTP-host”;
WebMail.SmtpPort = 25;
WebMail.EnableSsl = true;
WebMail.UserName = “your-user-name-here”;
WebMail.From = “your-email-address-here”;
WebMail.Password = “your-account-password”;
}
Sending the email is done using the Send method. Note this should be done with a try-catch block in case of failure.
@{
WebMail.Send(to: “me@dzone.com”,
subject: “Email subject”,
body: bodyContent
}
Caching Information
The WebCache helper provides caching functionality for your website.
Items are stored in the cache using a key.
@{
WebCache.Set(“myKey”, @DateTime.Now, 1, false);
}
Items are later retrieved using the same key.
@{
WebCache.Get(“myKey”);
}
Creating Your Own Helpers You can also create your own Helpers for use in your websites using the @helper or @function Razor syntax.
Debugging Tips
WebMatrix provides two helpers to assist with debugging: ServerInfo and ObjectInfo.
ServerInfo provides four separate views of information about the server environment.
TypeExampleServer ConfigurationInformation about the hosting web server, including the version of ASP.NET.ASP.NET Server VariablesDetails of HTTP variables, and values that are part of each web page request.HTTP Runtime VariablesDetails about the version of the .NET Framework that is running.Environment Variablesll the local environment variables and their values on the web server.
ObjectInfo displays the type and value of each object that is passed through to its print method:
ObjectInfo.Print (variable)
For further debugging, you can use Internet Explorer Developer Tools or FireBug.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}