Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Archive for February 4th, 2010

Publishing a Web Site

Posted by Ravi Varma Thumati on February 4, 2010

Provides step-by-step instructions for using the Publish Web Site utility of the Microsoft Visual Web Developer Web development tool to compile a Web site, and then copy the output to an active Web site.

If you want to deploy a finished Web site to a server, you can use the Publish Web Site utility that is included with the Microsoft Visual Web Developer Web development tool. The Publish Web Site utility precompiles the pages and code that are in the Web site and writes the compiler output to a folder that you specify. You can then copy the output to the target Web server and run the application from there.

Tasks illustrated in this walkthrough include the following:

  • Using the Publish Web Site utility to create precompiled output.

Prerequisites

In order to complete this walkthrough, you will need the following:

  • Visual Web Developer.
  • Access to Microsoft Internet Information Services (IIS) so that you can test the result of publishing a Web site.

In this walkthrough, it is assumed that you have IIS running on your own computer. Alternatively, you can use any instance of IIS for which you have permission to create a virtual directory.

Creating the Web Site

Create a new Web site and page by following these steps. For this walkthrough, you will create a file system Web site.

To create a file system Web site

  1. Open Visual Web Developer.
  2. On the File menu, click New Web Site.

The New Web Site dialog box appears.

  1. Under Visual Studio installed templates, click ASP.NET Web Site.
  2. In the left-most Location list, click File System.
  3. In the right-most Location list, enter the name of the folder where you want to keep the pages of the Web site.

For example, type the folder name C:\WebSites.

  1. In the Language list, click the programming language that you prefer to work in.
  2. Click OK.

Visual Web Developer creates the folder and a new page named Default.aspx.

Creating a Test Page and Class

For this walkthrough, you will create a Web page with some controls. You will also create a class file that you will use in the Web page. Creating both a Web page and a separate class will let you see how the publish process precompiles the contents of the Web site.

You will start by creating a new page, and then adding a button and label to the page.

To create the page and add controls

  1. In Solution Explorer, right-click the name of the Web site and click Add New Item.
  2. Under Visual Studio installed templates, click Web Form.
  3. In the Name box, type SamplePage.aspx.
  4. In the Language list, click the programming language that you prefer to work in.
  5. Click Add.
  6. Switch to Design view.
  7. From the Standard group in the Toolbox,drag a Labelcontrol onto the page.
  8. From the Standard group in the Toolbox, drag a Button control onto the page and position it next to the Label control.

Next, you will create the source code for a simple class that has a single property in it. You will use the class in the code for your page.

To create a class

  1. In Solution Explorer, right-click the name of the Web site, point to Add ASP.NET Folder, and then click App_Code.

A new folder named App_Code appears in your application in Solution Explorer. The App_Code folder is a special reserved ASP.NET application folder.

  1. Right-click the App_Code folder, and then click Add New Item.
  2. Under Visual Studio installed templates, click Class.
  3. In the Name box, type TestClass.
  4. In the Language list, click the programming language that you prefer to work in.
 
The programming language that you select does not have to be the same as the programming language that you use in the .aspx page.
  1. Click Add.

Visual Web Developer creates a skeleton class file in the programming language that you have specified. Notice that the extension of the class file name matches the language that you have selected. For example, if you are creating a class in Microsoft Visual Basic, the file name extension is .vb.

  1. Create a property named TestProperty.

When you are finished, the complete class file will look similar to the following:

Visual Basic

Imports Microsoft.VisualBasic

    Public Class TestClass

    Private TestPropertyValue As String

    Public Property TestProperty() As String

        Get

            Return TestPropertyValue

        End Get

        Set(ByVal value As String)

            TestPropertyValue = value

        End Set

    End Property

End Class

C#

using System;

public class TestClass

{

    public TestClass() { }

    private string TestPropertyValue;

    public string TestProperty

    {

        get{ return TestPropertyValue; }

        set{ TestPropertyValue = value; }

    }

}

Now, you can use the class in the page. Notice that you do not have to compile the class file before using it.

To use the class in the page code

  1. Open SamplePage.aspx and switch to Design view.
  2. Double-click the Button control to create a Click handler for it.
  3. In the Click handler, create an instance of the TestClass that you created in the preceding procedure, assign a value to the TestProperty property, and then display the TestProperty value in the Label control.

The complete code will look similar to this:

Visual Basic

Protected Sub Button1_Click(ByVal sender As Object, _

    ByVal e As System.EventArgs)

    Dim testClass As New TestClass

    testClass.TestProperty = “Hello”

    Label1.Text = testClass.TestProperty

End Sub

C#

protected void Button1_Click(object sender, EventArgs e)

{

    TestClass testClass = new TestClass();

    testClass.TestProperty = “Hello”;

    Label1.Text = testClass.TestProperty;

}

Testing the Site

Before publishing the site, you can test it to make sure that the site works in the way that you expect.

To test the Web site

  1. Open the SamplePage.aspx page.
  2. Press CTRL+F5.

The page appears in the browser.

  1. Click Button and make sure that text appears in the Label control.
  2. Close the browser.

Publishing the Web Site

Now that you have a Web site, you can publish it. You can publish the Web site to any location that you have access to on the local computer or on a network that is using any connection protocol that is supported by Visual Web Developer. You have the following options for copying the Web site:

  • Use a UNC share to copy to a shared folder that is on another computer on the network.
  • Use FTP to copy to a server.
  • Use the HTTP protocol to copy to a server that supports FrontPage 2002 Server Extensions from Microsoft.

In this part of the walkthrough, you will publish the Web site to local folder.

To publish the Web site

  1. On the Build menu, click Publish Web Site.

The Publish Web Site dialog box appears.

  1. In the Target Location box, enter c:\CompiledSite.
Caution:
All data in the target folder and its subfolders will be deleted. Make sure that you do not type the name of a folder that contains data or contains subfolders with data.
  1. For the purposes of this walkthrough, you are publishing to a local folder. You could also publish to a UNC share. If you wanted to publish to a remote Web site using HTTP or FTP, the Target Location box is where you would specify the remote server URL.
  2. The Allow this precompiled site to be updatable option specifies that all program code is compiled into assemblies, but that .aspx files (including single-file ASP.NET Web pages) are copied as-is to the target folder. In this walkthrough, you will not select that option.
  3. Click OK.

Visual Web Developer precompiles the contents of the Web site and writes the output to the folder that you specified. The Output window displays progress messages. If an error occurs during compilation, it is reported in the Output window.

  1. If errors occur during publishing, fix the errors, and then repeat step 1.

Examining the Output of the Publish Web Site Command

It is useful to examine the output of the Publish Web Site command so that you can see what Visual Web Developer has done with your Web site files.

To examine the output of the Publish Web Site command

  1. In Windows Explorer, move to the folder that you specified as the target for the Publish Web Site command.
  2. Using a text editor, such as Notepad, open the SamplePage.aspx file.

Notice that the file does not contain the markup that you originally had in the file. Instead, the .aspx page is only a placeholder that can be used as part of a URL.

  1. Move to the Bin folder.

The folder contains two types of files:

  • .compiled files, which correspond to pages.
  • .dll files, which contain the executable code for the Web site, such as the class file that you created.

Remember that the page, its code, and the separate class file that you created have all been compiled into executable code.

Testing the Published Web Site

You can now test the published Web site by running it.

To test the published Web site

  1. Create an IIS virtual directory that points to the target folder.

You can use the IIS administrative tools or alternatively, use the following steps:

  1. In Windows Explorer, right-click the name of the target folder, and then click Sharing and Security.
  2. On the Web Sharing tab, click Share this Folder.

The Edit Alias dialog box appears.

  1. If you want, change the name of the alias.

The default permissions allow Read access and allow Scripts, such as ASP.NET pages, to run.

  1. Click OK to close the Edit Alias dialog box, and then click OK to close the Properties dialog box.
  2. Open the browser and type the following URL:

http://localhost/CompiledSite/SamplePage.aspx

The SamplePage.aspx page appears. However, this time you are viewing the version of the page that was created by the precompiler for deployment.

Posted in 1. Microsoft.NET | Tagged: | 1 Comment »