Using ASP.NET Web Pages Helpers in ASP.NET MVC
Join the DZone community and get the full member experience.Join For Free
ASP.NET MVC 3 includes some helper methods of ASP.NET Web Pages technology that are used for common functionality. These helper methods includes: Chart, Crypto, WebGrid, WebImage and WebMail. For details of these helper methods, please see ASP.NET MVC 3 Beta Release Notes. However there are some other helper methods present in ASP.NET Web Pages and may also be useful for you like Google Analytic, Bing Search, Twitter Profile, Facebook Like etc. For details of all ASP.NET Web Pages helper methods, please see ASP.NET API Quick Reference. In this article, I will show you how to use some of these helper methods in ASP.NET MVC.
Before starting this article, I would like to mention that the technique in this article will also work in ASP.NET MVC 2 and Web Forms but some helpers like Facebook.LikeButton is dependent upon ASP.NET Web Pages due to which some helpers will not work in ASP.NET MVC 2 and Web Forms but work in ASP.NET MVC 3. So let's create an ASP.NET MVC 3 application. After creating the application, just add a reference of Microsoft.Web.Helpers.dll. This assembly will be found in WebMatrix directory when you install WebMatrix. If you don't have WebMatrix installed then don't worry. This assembly is included in the downloadable project.
Please note that the above project was built with ASP.NET MVC 3 Beta which includes the old version of Microsoft.Web.Helpers.dll. So if you are using ASP.NET MVC RC or any other new version then get the new version of Microsoft.Web.Helpers.dll by right clicking on solution explorer then Add Library Package Reference then search microsoft-web-helpers. In this new version Facebook class is no longer available. Also the new version uses HttpContext.Current instead of WebPageContext.Current.HttpContext which was used in previous version.
Then just open Index View and add the following lines,
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
03 <%@ Import Namespace="Microsoft.Web.Helpers" %>
04 <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
05 Home Page
07 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
09 <tr valign="top">
14 <%=Twitter.Search("ASP.NET MVC")%>
17 <tr valign="top">
22 <%= LinkShare.GetHtml("Imran Baloch's Blog", "http://weblogs.asp.net/imranbaloch")%>
First of all I have imported the namespace Microsoft.Web.Helpers which includes the ASP.NET Web Pages helper methods. Then I simply added Twitter.Profile, Twitter.Search, Facebook.LikeButton and LinkShare.GetHtml helper methods. It looks very simple. Now just run this application. You will find the following screen,
In this article I showed you how you can leverage the ASP.NET Web Pages helper methods in your ASP.NET MVC application. This will make it easy for you to add social engineering, Bing search, share links etc, into your site quickly. Hopefully you will enjoy this article too. You can also download the sample application which also include the assembly of these helper methods.
Published at DZone with permission of Imran Baloch, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.