Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using ASP.NET Web Pages Helpers in ASP.NET MVC

DZone's Guide to

Using ASP.NET Web Pages Helpers in ASP.NET MVC

·
Free Resource
Introduction:

          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. 

 

 Description:

          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.

 UPDATE: 

          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" %>
02
03 <%@ Import Namespace="Microsoft.Web.Helpers" %>
04 <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
05 Home Page
06 </asp:Content>
07 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
08 <table>
09 <tr valign="top">
10 <td>
11 <%=Twitter.Profile("scottgu")%>
12 </td>
13 <td>
14 <%=Twitter.Search("ASP.NET MVC")%>
15 </td>
16 </tr>
17 <tr valign="top">
18 <td>
19 <%=Facebook.LikeButton("http://weblogs.asp.net/imranbaloch")%>
20 </td>
21 <td>
22 <%= LinkShare.GetHtml("Imran Baloch's Blog", "http://weblogs.asp.net/imranbaloch")%>
23 </td>
24 </tr>
25 </table>
26 </asp:Content>


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,

 

 

          

 

 

 Summary:

          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.

Topics:

Published at DZone with permission of Imran Baloch, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}