One effective way of driving traffic to your Web site is by creating an affiliate program. For example, Amazon has an affiliate program for books. You can list books on your Web site that link back to the Amazon.com Web site. Every time a person buys a book after linking to Amazon through your Web site, you get a small commission. In this section, you'll build the infrastructure to create an affiliate program for the ASP.NET job site. To do this, you must create an XML Web Service and two custom controls. The custom controls work with the XML Web Service to display the most recent jobs and resumes listed at the job site. These controls can be distributed to affiliate Web sites to display current job and resume listings. The ASP.NET job site exposes an XML Web Service called the JobService . This Web Service has two methods : GetNewJobs and GetNewResumes . The GetNewJobs method returns a DataSet that represents the most recent 50 jobs listed and the GetNewResumes method returns a DataSet that represents the most recent 50 resumes listed. NOTE To learn more about XML Web Services see Part VI, "Building ASP.NET Web Services." The complete code for the XML Web Service is contained in Listing 30.9. Listing 30.9 Site\Services\JobService.asmx<%@ WebService Language="vb" Class="myServices.JobService" %> Imports System Imports System.Web.Services Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Namespace myServices <WebService(Namespace:="http://yourdomain.com/webservices/")> _ Public Class JobService Inherits WebService <WebMethod()> Function GetNewJobs() As DataSet Dim strConString As String Dim conJobs As SqlConnection Dim dadJobs As SqlDataAdapter Dim dstJobs As DataSet dstJobs = Context.Cache( "NewJobs" ) If dstJobs Is Nothing Then ' Ready Database Connection strConString = ConfigurationSettings.AppSettings( "constring" ) conJobs = New SqlConnection( strConString ) dstJobs = New DataSet() ' Get Newest 50 Jobs From Database dadJobs = New SqlDataAdapter( "getNewJobs", conJobs ) dadJobs.SelectCommand.CommandType = CommandType.StoredProcedure dadJobs.Fill( dstJobs, "NewJobs" ) Context.Cache( "NewJobs" ) = dstJobs End If Return dstJobs End Function <WebMethod()> Function GetNewResumes() As DataSet Dim strConString As String Dim conJobs As SqlConnection Dim dadResumes As SqlDataAdapter Dim dstResumes As DataSet dstResumes = Context.Cache( "NewResumes" ) If dstResumes Is Nothing Then ' Ready Database Connection strConString = ConfigurationSettings.AppSettings( "constring" ) conJobs = New SqlConnection( strConString ) dstResumes = New DataSet() ' Get Newest 50 Resumes From Database dadResumes = New SqlDataAdapter( "getNewResumes", conJobs ) dadResumes.SelectCommand.CommandType = CommandType.StoredProcedure dadResumes.Fill( dstResumes, "NewResumes" ) Context.Cache( "NewResumes" ) = dstResumes End If Return dstResumes End Function End Class End Namespace The C# version of this code can be found on the CD-ROM. To access the XML Web Service in Listing 30.9, you can enter the following URL in the Address Bar of your Web browser: http://localhost/aspnetjobs/site/services/jobservice.asmx Included with the job site are two custom controls, named NewJobs and NewResumes , which interact with the XML Web Service to display a list of new jobs and resumes. The custom controls are located in an assembly named NewListings.dll , which can be found in the /bin directory of the job site. You can distribute these controls to other affiliate Web sites. The affiliate Web sites can use these controls in their ASP.NET pages to display links to jobs at your Web site (see Figure 30.7). Because the controls retrieve the list of new jobs and resumes from an XML Web Service, the list of jobs and resumes displayed by the controls is always current. Figure 30.7. The NewJobs and NewResumes controls.
The complete code for the NewJobs and NewResumes controls is contained in Listing 30.10. (This file can be found in the aspnetjobs\site\services subdirectory of the job site.) Listing 30.10 NewListings.vbImports myControls Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.Services Imports System.Data Namespace myControls Public Class NewJobs: Inherits WebControl Const aspnetjobsUrl = "http://localhost/aspnetjobs/" Public PageSize AS Integer = 5 Protected Overrides Sub CreateChildControls Dim dgrdNewJobs As DataGrid Dim objBoundColumn As BoundColumn Dim objHyperLinkColumn As HyperLinkColumn ' Build DataGrid dgrdNewJobs = New DataGrid dgrdNewJobs.ID = "NewJobsGrid" dgrdNewJobs.AllowPaging = True dgrdNewJobs.PageSize = PageSize dgrdNewJobs.PagerStyle.Visible = False dgrdNewJobs.Cellpadding = 4 dgrdNewJobs.BorderStyle = BorderStyle.Solid dgrdNewJobs.AutoGenerateColumns = False dgrdNewJobs.ShowHeader = False dgrdNewJobs.GridLines = GridLines.None ' Add Date Column objBoundColumn = New BoundColumn() objBoundColumn.DataField = "job_entrydate" objBoundColumn.DataFormatString = "{0:m}" dgrdNewJobs.Columns.Add( objBoundColumn ) ' Add HyperLink Column objHyperLinkColumn = New HyperlinkColumn objHyperLinkColumn.DataTextField = "job_briefdesc" objHyperLinkColumn.DataNavigateUrlField = "job_id" objHyperLinkColumn.DataNavigateUrlFormatString = _ aspnetjobsUrl & "site/job.aspx?id={0}" dgrdNewJobs.Columns.Add( objHyperLinkColumn ) ' Bind DataGrid dgrdNewJobs.Datasource = GetNewJobs() dgrdNewJobs.Databind() ' Add Child Controls Controls.Add( New LiteralControl( "<b>New Jobs!<b>" ) ) Controls.Add( dgrdNewJobs ) End Sub Private Function GetNewJobs() As DataSet Dim dstJobs As DataSet Dim objJobService As JobService dstJobs = Context.Cache( "JobService_NewJobs" ) If dstJobs Is Nothing Then objJobService = New JobService dstJobs = objJobService.GetNewJobs() Context.Cache.Insert( _ "JobService_NewJobs", _ dstJobs, _ Nothing, _ DateTime.Now.AddMinutes( 15 ), _ Timespan.Zero ) End If Return dstJobs End Function End Class Public Class NewResumes : Inherits WebControl Const aspnetjobsUrl = "http://localhost/aspnetjobs/" Public PageSize AS Integer = 5 Protected Overrides Sub CreateChildControls Dim dgrdNewResumes As DataGrid Dim objBoundColumn As BoundColumn Dim objHyperLinkColumn As HyperLinkColumn ' Build DataGrid dgrdNewResumes = New DataGrid dgrdNewResumes.ID = "NewResumesGrid" dgrdNewResumes.AllowPaging = True dgrdNewResumes.PageSize = PageSize dgrdNewResumes.PagerStyle.Visible = False dgrdNewResumes.Cellpadding = 4 dgrdNewResumes.BorderStyle = BorderStyle.Solid dgrdNewResumes.AutoGenerateColumns = False dgrdNewResumes.ShowHeader = False dgrdNewResumes.GridLines = GridLines.None ' Add Date Column objBoundColumn = New BoundColumn() objBoundColumn.DataField = "ul_entrydate" objBoundColumn.DataFormatString = "{0:m}" dgrdNewResumes.Columns.Add( objBoundColumn ) ' Add HyperLink Column objHyperLinkColumn = New HyperlinkColumn objHyperLinkColumn.DataTextField = "ul_briefdesc" objHyperLinkColumn.DataNavigateUrlField = "ul_username" objHyperLinkColumn.DataNavigateUrlFormatString = _ aspnetjobsurl & "{0}.aspx" dgrdNewResumes.Columns.Add( objHyperLinkColumn ) ' Bind DataGrid dgrdNewResumes.Datasource = GetNewResumes() dgrdNewResumes.Databind() ' Add Child Controls Controls.Add( New LiteralControl( "<b>New Resumes!<b>" ) ) Controls.Add( dgrdNewResumes ) End Sub Private Function GetNewResumes() As DataSet Dim dstResumes As DataSet Dim objJobService As JobService dstResumes = Context.Cache( "JobService_NewResumes" ) If dstResumes Is Nothing Then objJobService = New JobService dstResumes = objJobService.GetNewResumes() Context.Cache.Insert( _ "JobService_NewResumes", _ dstResumes, _ Nothing, _ DateTime.Now.AddMinutes( 15 ), _ Timespan.Zero ) End If Return dstResumes End Function End Class End Namespace The C# version of this code can be found on the CD-ROM. Both the NewJobs and NewListings controls inherit from the WebControl class. The NewJobs control contains one subroutine and one function. The subroutine overrides the CreateChildControls method of the base control class. This subroutine is used to add a DataGrid control to the custom control. The DataGrid control is bound to the DataSet returned by the GetNewJobs function. The GetNewJobs function uses the JobService Web Service to retrieve a DataSet . Notice that the NewJobs control caches the DataSet in the Cache object to improve performance. The control retrieves a new list of jobs from the JobService Web Service every 15 minutes. When the jobs listings are displayed by the NewJobs control, each listing contains a link back to the ASP.NET job site. The NewJobs control contains a property, named PageSize , which determines how many job listings are displayed by the control. CAUTION Before you compile the NewListings control, you must modify the aspnetjobsUrl constants that appear at the top of both the NewJobs and NewResumes classes. These constants specify the location of the ASP.NET job site. The NewResumes control is similar to the NewJobs control. It also overrides the CreateChildControls subroutine to display a DataGrid . The NewResumes control uses the GetNewResumes function to retrieve a list of new resumes from the JobService Web Service every 15 minutes. Both the NewJobs and NewResumes controls depend on a proxy class for the JobService Web Service. Before you can compile the source code for the controls, you must first create a proxy class for the JobService Web Service. To create and compile the proper proxy class, you can execute the MakeJobServiceProxy.bat batch file contained in Listing 30.11. CAUTION Before you compile the JobService proxy class, modify the MakeJobServiceProxy.bat file to point to the location of the JobService Web Service on your server. You'll need to replace the phrase "localhost" with the proper domain name for your Web site. Listing 30.11 Site\Services\MakeJobServiceProxy.bat [View full width] Wsdl.exe /l:VB /n:myControls http:// localhost /aspnetjobs/site/services/jobservice.asmx?wsdl vbc /t:library /r:System.dll,System.Web.Services.dll,System.data.dll,System.XML.dll Jobservice.vb copy JobService.dll ..\..\bin pause The C# version of this code can be found on the CD-ROM. The batch file in Listing 30.11 creates a compiled class named JobService.dll . After the JobService.dll proxy class is created, it is copied to the /bin directory of your application. After you create the proxy class, you can compile the NewJobs and NewResumes custom controls contained in Listing 30.10. Execute the batch file in Listing 30.12 to compile these controls. Listing 30.12 Site\Services\MakeNewListings.bat [View full width] vbc /t:library /r:System.dll,System.Web.dll,System.Web.Services.dll,System.Xml.dll,System .Data.dll,Jobservice.dll NewListings.vb copy NewListings.dll ..\..\bin pause The C# version of this code can be found on the CD-ROM. After you compile the NewJobs and NewResumes controls, you can distribute the controls to any site on the Internet. An affiliate Web site simply needs to copy the NewListings.dll file into its application /bin directory. The page in Listing 30.13 illustrates how you can use these controls in an ASP.NET page located at another Web site. Listing 30.13 Site\Services\Affiliate.aspx<%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="NewListings" %> <html> <head><title>Affiliate.aspx</title></head> <body> <myControls:NewJobs PageSize="10" Runat="Server" /> <p> <myControls:NewResumes PageSize="10" Runat="Server" /> </body> </html> The C# version of this code can be found on the CD-ROM. The page in Listing 30.13 contains a Register page directive to register the NewListings custom controls for the current page. After the controls are registered, they can be used exactly like any of the standard ASP.NET controls. In Listing 30.13, the PageSize property of both the NewJobs and NewResumes controls are set to display a maximum of 10 listings. |