Installing a Helper in an ASP.NET Web Pages (Razor) Site
This article describes how to install a helper in an ASP.NET Web Pages (Razor) website. A helper is a reusable component that includes code and markup to perform a task that might be tedious or complex.
What you'll learn:
- How to install a helper in a website created using WebMatrix 2 RC or WebMatrix 1.0.
Note The information in this article applies to ASP.NET Web Pages 1.0 and Web Pages 2, and to Microsoft WebMatrix 1.0 and Microsoft WebMatrix 2.
Overview of Helpers
Some tasks that people often want to do on web pages require a lot of code or require extra knowledge. Examples include displaying a chart for data; putting a Facebook "Like" button on a page; sending email from your website; cropping or resizing images; using PayPal for your site. To make it easy to do these kinds of things, ASP.NET Web Pages lets you use helpers. Helpers are components that you install for a site and that let you perform typical tasks by using just a line or two of Razor code.
For example, in order to display a Facebook "Like" button on a page, you can
method, like this:
When the page runs, the
ASP.NET Web Pages has a few helpers built in. However, many helpers are available in packages (add-ins) that are provided using the NuGet package manager. NuGet lets you select a package to install and then it takes care of all the details of the installation.
This article provides separate instructions for how to install a helper in WebMatrix 2 and in WebMatrix 1.0:
Installing a Helper in WebMatrix 2
In WebMatrix 2, click the Gallery button.
In the search box, enter a keyword for the helper you want to install (for example, "Twitter").
NuGet shows the packages that meet your search criteria.
Select the package and then click Install.
When installation of the helper is done, you see a message in the notification area at the bottom of the screen.
If this is the first time you've installed a helper, NuGet creates folders in your website for the code that makes up the helper.
To uninstall a helper, click the Gallery button, click the Installed tab, and pick the package you want to uninstall.
This launches the NuGet package manager and displays available packages.
Installing a Helper in WebMatrix 1.0
In WebMatrix, click the Site workspace.
In the content pane, click ASP.NET Web Pages Administration. This loads an administration page into your browser. Because this is the first time you're logging into the administration page, it prompts you to create a password.
Create a password.
After you click Create Password, a security-check page that looks like the following prompts you to rename the password file for security reasons. If this is the first time you're seeing this page, don't try to rename the file yet. Proceed to the next step and follow the directions there.
Leave the security-check page open in the browser, return to WebMatrix, and then click the Files workspace.
Right-click the Hello World folder for your site and then click Refresh. The list of files and folders now displays an App_Data folder. Open that and you see an Admin folder. The newly created password file (_Password.config) is displayed in the ./App_Data/Admin/ folder. The following illustration shows the updated file structure with the password file selected:
Rename the file to Password.config by removing the leading underscore (_) character.
Return to the security-check page in the browser, and click the Click Here link near the end of the message about renaming the password file.
Log into the Administration page using the password you created. The page displays the Package Manager, which contains a list of add-on packages.
If you ever want to display other feed locations, click the Manage Package Sources link to add, change, or remove feeds.
Find the ASP.NET Web Helpers Library package. To narrow down the list, search for helpers using the Search field. The following image shows the result of searching for helpers. Notice that several versions of this package are available.
Select the version that you want, click the Install button, and then install the package as directed. After the package is installed, the Package Manager displays the result.
This page also lets you uninstall packages, and you can use the page to update packages when newer versions are available. You can go to the Show drop-down list and click Installed to display the packages you have installed, or click Updates to display available updates for the installed packages.