Using Web Helpers in Razor – ASP.NET MVC 3 Beta
Posted by: Malcolm Sheridan ,
on 10/22/2010,
in
Category ASP.NET MVC
Abstract: The following article demonstrates how to use the Microsoft Web Helpers library in ASP.NET MVC 3 Beta.
The ASP.NET MVC team has recently released ASP.NET MVC 3 Beta. There have been some big changes in this release since MVC 2, and the biggest in my opinion has been the inclusion of the Razor View Engine. Razor simplifies your HTML mark-up and makes the code more readable. Razor contains the HTML helpers you know and love, such as Html.TextBox and Html.TextBoxFor. Well they have recently released a new library full of helpers and it is called the Microsoft Web Helpers library. This can be downloaded through NuPack. NuPack is a free open source package manager that makes it easy for you to find, install, and use .NET libraries in your projects. This article will show you how to download the library and start using some of these new helpers. This code also works in WebMatrix if you're using it.
Open Visual Studio 2010 and create a new ASP.NET MVC 3 Web Application (Razor) project. The new MVC 3 Beta dialog can be seen in Figure 1.
Figure 1: The new ASP.NET MVC 3 Beta Project dialog allows you to choose which view engine you want to work with.
Choose Razor as the view engine and click OK.
The next step is to download the Microsoft Web Helpers library through NuPack. Follow these steps to do this:
Step 1: Right click the website and choose Add Package Reference. See Figure 2.
Figure 2: The Add Package option from right-clicking the website.
Step 2: The Add Package Reference dialog will open. Search for Microsoft in the search box in the top right corner. Upon completion you'll see microsoft-web-helpers option. Select that option and click Install. See Figure 3:
Figure 3: Choose microsoft-web-helpers and click install to get started.
Step 3: Click ‘Close’ to return to the project
Check the references and you'll see that the library has been added to the project. Figure 4 displays the new reference.
Figure 4: A new reference to the System.Web.Helpers assembly
Now it's time to start coding! Some of the new helpers added are:
-
Twitter - allows you to search or display a Twitter profile
-
Bing - search a website through Bing
-
AntiForgery - renders anti forgery tokens to stop CSRF attacks
Twitter
The Twitter helper allows you to search on Twitter or display a profile. To search on Twitter use the following code:
@Twitter.Search("wdx")
To search a profile use the following code:
@Twitter.Profile("malcolmsheridan")
Figure 5 shows you the Twitter helper.
Figure 5: Twitter search on your website.
Bing
The Bing helper allows you to search Bing from your website. Just type in a search and the results are displayed on the page. Here's the code:
@Bing.SearchBox("http://www.asp.net")
The only parameter is the site you want to perform the search on. Figure 6 displays the results.
Figure 6: Bing searching enabled on your website
AntiForgery
The AntiForgery helper renders a unique hidden field in your form to defend yourself from CSRF attacks. A Salt value can be passed as the only parameter if you have multiple forms on your page that you want protected independently from each other. Here's the code below:
<form action="post" action="/Home/SomeAction">
@AntiForgery.GetHtml()
</form>
Adding Salt looks like this:
<form action="post" action="/Home/SomeAction">
@AntiForgery.GetHtml("someSaltValue")
</form>
Figure 7 shows the result of the unique token.
Figure 7: The unique hidden field that is generated by the AntiForgery helper.
As you can see these helpers are new to the Microsoft.Web.Helpers library and can be used in ASP.NET MVC 3 Beta as well as WebMatrix Beta 2. There are existing helpers that are great for you to know, so I'll cover them in a future article.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
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 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 Book 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 the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) 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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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