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

 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" %>

<%@ Import Namespace="Microsoft.Web.Helpers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table>
        <tr valign="top">
            <td>
                <%=Twitter.Profile("scottgu")%>
            </td>
            <td>
                <%=Twitter.Search("ASP.NET MVC")%>
            </td>
        </tr>
        <tr valign="top">
            <td>
                <%=Facebook.LikeButton("http://weblogs.asp.net/imranbaloch")%>
            </td>
            <td>
                <%= LinkShare.GetHtml("Imran Baloch's Blog", "http://weblogs.asp.net/imranbaloch")%>
            </td>
        </tr>
    </table>
</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.

 

11 Comments

  • Nice article, thanks for sharing!!!

  • it make work easy,thanks for sharing information

  • Can you write explanation for ReCaptcha in MVC 3 RC.

  • @Neno, please see
    http://www.asp.net/webmatrix/tutorials/18-customizing-site-wide-behavior

  • Attempt by method 'Microsoft.Web.Helpers.ReCaptcha.GetHtmlWithOptions(System.String, System.Object)' to access method 'System.Web.WebPages.WebPageContext.get_HttpContext()' failed
    Solution for this problem.

  • I install Microsoft Web Helpers using NuGet ,but error the same.
    Twitter,Gravatar,LinkShare hepers working bur reCaptchar,facebook not.

  • @AlenS,
    See the update comment above.
    Facebook is not available in new version.
    ReCaptcha is working,
    Just download the sample application here,
    http://www.speedfile.org/577709

  • I must say you saved one whole day of my research. Thanks a million...
    I wanted to know is there a way I can customize UI from blue to something which suits the website... Please let me know,
    Thanks,

  • I was referring to Twitter Search UI. Can that be Customized?

  • Hi ,I'm new in mvc ..and really it´s getting a bit hard..
    I'm trying to add a recaptcha control , the example given by iram at www.speedfile.org/577709 is great ! but i'm no able to implement the submit button , I had add a submit button and then I call to a method in the controller:

    public ActionResult SubmitCaptcha()
    {

    if (ReCaptcha.Validate(privateKey:"6Ld4iQsAAAAAAM3nfX_K0vXaUudl2Gk0lpTF3REf" ))
    {
    return RedirectToAction("LogOn");
    }

    return RedirectToAction("Index", "Home");
    }

    but validate is always false(even the text words are correct) and the private key i have added first in a :viewstart.cshtml (layout element..)

    any help please??!!

  • @Albert,
    I have just checked it is working fine,
    @{
    Microsoft.Web.Helpers.ReCaptcha.PublicKey = "6LcQbb8SAAAAAK4H_t4XFeVpD1dg5UpPgLp18dD0";
    Microsoft.Web.Helpers.ReCaptcha.PrivateKey = "6LcQbb8SAAAAAJZROm5wXbGhjFmY__k7VHABwG2O";
    }
    @using (Html.BeginForm()) {
    @Microsoft.Web.Helpers.ReCaptcha.GetHtml()

    }

    if (ReCaptcha.Validate("6LcQbb8SAAAAAJZROm5wXbGhjFmY__k7VHABwG2O"))
    return Content("Correct");
    return Content("Wrong");
    Note that these are temporary keys, must be replace with your site keys during deployment

Comments have been disabled for this content.