It’s nearly the weekend, and it’s been long week, so let’s have a bit of fun…
So I take it you’ve all had a chance to look at WebMatrix? If you haven’t then you should really make the effort to have a play around with it. I will admit that I was a bit snobby about it when I first read about it, but it is actually a really great way to build small, simple web sites. For the uninitiated, here are a few useful links:
You can install WebMatrix through the Web Platform Installer. It has loads of really great features, which are much better explained in the links above than I could ever do, but the one that hit me immediately as being really fun was the WebMatrix Helpers feature and I wondered if there was a way to use them in MVC.
Well, WebMatrix is built on ASP.NET, so it is actually a trivial task to add them to an ASP.NET MVC application.
There are loads of helpers available, today we are going to look at the Microsoft Web Helpers. This package gives you the ability to quickly and easily add basic functionality for services such as Twitter, Bing, Gravatar, Facebook, Google Analytics and XBox Live to your site. The awesomeness of NuGet makes it easy to add the Microsoft Web Helpers package to our project by typing:
into the the Package Manager Console.
Next you will need to add references to WebMatrix.Data and WebMatrix.WebData and set the “Copy Local” property to true for both of them. If you fail to do this step you will receive a compilation error, “The type or namespace name ‘SimpleMembershipProvider’ could not be found” in App_Code\Facebook.cshtml.
And that’s it! You are now ready to start using the Web Helpers in your Views. So let’s have a look at a few of them…
You can add a Gravatar image to your page by simply using the
Gravatar.GetHtml() method in your Razor view. For example:
displays this handsome chap (and no the glasses and bandito moustache are not real!):
If the email address supplied to the method doesn’t have a corresponding Gravatar account the default Gravatar image will be returned, i.e.
But you can also set the default image to be the URL of any image you desire. For example, the following code:
("firstname.lastname@example.org", defaultImage: "http://blog.stevelydford.com/content/noGrav.jpg")
Returns this stunning example of programmer art:
Optional parameters allow you to set the size of the image, the Gravatar rating and a couple of other attributes.
XBox Live GamerCard
This is a very simple method which returns an XBox Live GamerCard. It has one parameter which is a string containing the required XBox Live GameTag. For example:
which does nothing if not prove that I don’t get enough time to work on my GamerScore!
Web Helpers has a Bing class that enables you to easily let users Google your site with Bing.
First, we need to add the following code to our Razor view:
Bing.SiteTitle = ".NET Web Stuff, Mostly";
Bing.SiteUrl = "http://blog.stevelydford.com";
We can then use either the
Bing.AdvancedSearchBox() methods to display a Bing search box in our View.
displays a Bing search box which takes the user to Bing.com to displays it’s results:
displays a Bing search box which renders a <div> on your page containing the search results:
The Analytics class of Microsoft.Web.Helpers contains methods which generate scripts that enable a page to be tracked by Google Analytics, Yahoo Marketing Solutions and/or StatCounter.
They all work in a very similar way and just require you to pass the method the relevant account details. For example:
The Microsoft.Web.Helpers namespace contains a Twitter class with two methods –
There are a whole raft of parameters, which allow you to customise the output in various ways, such as setting the width and height, colors, number of tweets returned, etc. A full list of these parameters can be found here.
Twitter.Search() displays the Twitter search results for the search string specified in the
Again, there are a bunch of optional parameters to allow you to customize the output to your requirements.
When you use NuGet to install the microsoft-web-helpers package a TwitterGoodies Razor file is added to your App_Code folder. This class contains helpers which provide additional Twitter functionality. These helpers include
TwitterGoodies.TweetButton(), TwitterGoodies.FollowButton(), TwitterGoodies.Faves() and
TwitterGoodies.List(), all of which can have their outputs customised using various optional parameters:
(tweetText: "I'm reading Steve Lydford's blog", url:"http://blog.stevelydford.com")
displays a Tweet Button which opens a new window to allow the user to send a Tweet about your site. The URL passed to the helper is automatically shortened using the Twitter t.co URL shortner:
displays a button which redirects them to Twitter:
displays a form which shows a public Twitter list:
There are a few other methods in the TwitterGoodies Razor file, which you can view in App_Code/TwitterGoodies.cshtml.
As well as the TwitterGoodies.cshtml page, the microsoft-web-helpers package also installs Facebook.cshtml to your App_Code directory. This file contains many useful Facebook helpers. I will look at a couple here, a full list can be found on the facebookhelper codeplex site.
One of the easiest to use out of the box is the
Facebook.LikeButton() helper, which displays a ‘Like’ button that either automatically ‘likes’ the URL supplied, or opens a new Facebook window ’on click’ if the user is not currently signed in:
Next up is
Facebook.ActivityFeed() which displays stories when users ‘like’ content on a site or share content from a site on Facebook.
Most of the rest of the Facebook helpers require initialization. In order to do this you will require a Facebook Application ID. You can get one by browsing to http://www.facebook.com/developers/createapp.php and creating a new Facebook Application:
When you are setting up your app ensure that you enter the URL of your site, including the correct port number if you are working on localhost:
You can then add the following code to your Razor view to initialize:
Then it’s just a matter of adding a couple of lines of code to the view to add Facebook Comments to the page:
Or, to show a Facebook LiveStream to allow users of your site to communicate in real time:
This post shows just a small fraction of what can be achieved very quickly and very easily using WebMatrix Web Helpers in an MVC application. The Microsoft Web Helpers package makes it incredibly easy to add a whole load of functionality to you site for very little effort.
Have fun! Let me know how you get on.