Adding Search to an ASP.NET Web Pages (Razor) Site
This article explains how to use a helper to add Bing search capabilities to pages in an ASP.NET Web Pages (Razor) website.
What you'll learn:
- How to add the ability to search a website (including your own) to your website.
This is the ASP.NET feature introduced in the article:
- The
Binghelper.
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.
Searching from Your Website
By adding the capability to search the web from your website, you can include Internet search results without leaving your site. Adding search to your site can be useful in these ways:
- Add a "Search this site" box that lets users search your site (that is, the current site). This makes it easy for users to find content on your site.
- Add a box that lets users easily search related sites. For example, if your site is for a school sports team, you could add a search box that lets users also search the school's website.
- Add a box that lets users search the web, but without having to leave your site to launch a search in another window.
To add search to your site, you use the Bing helper and (optionally) specify the URL of the site to search. The
Bing helper renders a text box where users can enter a search term.
The Bing helper renders a box that includes
the Bing search icon that users can click in order to launch the search:

If you've specified a site to search, the helper also renders radio buttons that let the user specify whether to search only the specified site or the web in general. When the user submits the search, the helper redirects the search to the Bing site (http://bing.com). The results are displayed in a new browser window, as if the user had entered the search term in the Bing home page:

In this procedure, you create a web page that shows how to
use the Bing search helper which displays a custom search title and
that can search the www.asp.net site.
- Create a new website.
- Add the ASP.NET Web Helpers Library to your website as described in Installing Helpers in an ASP.NET Web Pages Site, if you haven't already added it.
-
Add a new page named Search.cshtml and add the following markup:
@{ Bing.SiteUrl = "www.asp.net"; Bing.SiteTitle = "ASP.NET Custom Search"; } <!DOCTYPE html> <html> <head> <title>Bing Search Box</title> </head> <body> <div> <h1>Bing Search</h1> <p>Search displays results by opening a new browser window that shows the Bing home page with search results.</p> Search the ASP.NET site: <br/> @Bing.SearchBox() </div> </body> </html>In the code, you call the
Binghelper. TheSearchBoxmethod uses the optionalsiteUrlparameter, which lets you specify which site to search. (If you don't specify a URL, Bing just searches the web.) In this case, you're searching the www.asp.net website. If you wanted to search your own site, you'd substitute that URL forwww.asp.net. - Run the Search.cshtml page in a browser. (Make sure the page is selected in the Files workspace before you run it.)
Enter a search term in the box, and then click the Search button. The results are displayed in a new browser window.
Note In order for the
Binghelper to return results, the site you're searching must be publicly available and its contents must have been examined ("crawled") by Bing. If you add a "Search this site" box and configure theBinghelper to search your own site, you won't be able to test it until the site has been live long enough for search engines to have found it. In other words, you won't be able to test the search capability in WebMatrix directly.

Comments (0) RSS Feed