Resources

Creating Websites with ASP.NET

Creating Website

Visual Studio projects allow definition of new site based on how intend to access its content.

Steps...

  1. Use File | New | Web Site
  2. Select website type, location and programming language
  3. Select target .NET framework (2, 3, 3.5 or 4) - preferably 4
  4. Click OK to finish set-up

Can also create ASP.NET web application via File | New | Project and selecting ASP.NET Web Application. Web applications and websites function and perform similarly, but differ in several ways. With web application you can:

Website projects better choice where one developer will be creating and managing site. Web applications should be used in enterprise environments with multiple developers and formal processes.

File System Based Website

Keeps development local until ready to publishing

Choose "File System" from Web Location box in New Web Site dialogue box.

Visual Studio will create two pages - Default.aspx and About.aspx, associated code behind files and a master file called Site.master. An App_Data folder is created as well as a web.config file.

Creating on a server

Manually

IIS 7.5 does not support Front Page Server Extensions as it used WebDAV natively. Therefore can't use HTTP to create website, but can use file-system based or FTP (former being preferred).

On server...

  1. Install .NET 4 on server
  2. Install IIS 7.5 with ASP.NET role
  3. Open IIS manager. Create new website that uses ASP.NET 4 application pool
  4. Grant web developer user account NTFS write permissions to website folder
  5. Share website folder, grant web developer accounts Change share permissions.

On development machine...

  1. Map network drive to shared folder
  2. Use Visual Studio to create file system based website, specifying drive letter of the shared folder.

WebDAV

Can publish via HTTP using WebDAV

On server...

  1. Follow previous steps, but do not share website folder. Instead configure WebDAV as follows...
  2. Add WebDAV Publishing role to IIS
  3. In IIS Manager add WebDav Authoring Role to grant user accounts access to Read, Write and Source for website
  4. Enable WebDAV on server-side

On development machine...

  1. Map network drive to websites URL
  2. Use Visual Studio to create file system based website, specifying drive letter of the network drive.

HTTP Based Website

Can publish files using file system based location (see previous section). If using Windows 7 or Server 2008 can create website via HTTP.

Before creating site must have following windows features:

To create website using IIS:

  1. Launch Visual Studio as an Administrator
  2. Create a new website
  3. Select website type and language
  4. Choose HTTP as Web Location
  5. Set Local IIS as location for site
  6. Click Create New Web Applications

If connecting to remote server then remote machine must have .NET Framework 4, ASP.NET and Front Page Server Extensions enabled. Note, Front Page Server Extensions are not supported on IIS 7.5 and Windows 2008 R2 - on these environments can create website via HTTP deployment.

FTP Based Website

On server...

  1. Configure server as in File System Based Website
  2. Install FTP server role service on server hosting website

On client...

  1. Use Visual Studio to create new website
  2. From Web Site Type drop-down select FTP
  3. When OK is clicked choose between active / passive mode and provide login credentials

Only use when host does not support WebDAV or Front Page Server Extensions. FTP sends user credentials unencrypted - remember to regularly change them.

Website Solution Files

When website created solution (.sln) file and hidden solution user options (.suo) file generated. By default these are in My Documents\Visual Studio 2010\Projects, even if the actual root folder is elsewhere.

.sln file is xml containing:

.suo is binary file of user settings related to IDE. Contains

Solution files not located in websites folder as they are specific to Visual Studio and are not required in deployed website. A solution can contain many websites and other project types.

A visual studio website does not contain an associated project file, although a eb application does.

Website content

Default ASP.NET 4 Website Template Default Objects

Object Name  Description
Account User management forms for registering new users, logging in and changing password
APP_DATA Application data files if accessing local database
Scripts Client side jQuery and JavaScript used by some ASP.NET components
Styles Request list of supported commands.
About.aspx Default about page
Default.aspx Default page that appears when user accesses site without specifying page
Global.asax Code for Application and Session events. Includes Application.Start, Application.End, Application.Error, Session.Start, Session.End
Site.master Defines header for default site pages - including menu structure
Web.config Site configuration file

 

Right clicking site in Solution Explorer can add following ASP.NET Folders (which are protected by ASP.NET)

Folder Name  Description
App_Browsers Custom browser definition files used by ASP.NET to identify browsers
App_Code Source code for classes developer intends to compile into application
App_Global_Resources Resources compiled into satellite assemblies and having global scope
App_Local_Resources Resources scoped to specific page, user control or master page within application
App_Themes Sub folders each defining specific theme for site
App_Web_References Contains references to web services
Bin Compiled assemblies that application requires to execute

 

Creating ASPX files

An ASP.NET page (also called web form) is composed of a single or pair of files.

ASPX page has two components - layout and code. Layout is expressed using HTML. Code is written in VB or C#.

Both these components can be stored in a single file or in separate files (known as code-behind).

Code-behind is method of choice - easy to organise, widely accepted and default method used by Visual Studio.

If using single file then the code is contained within a script block that is tagged as running at the server, e.g.

<script runat="server">
protected void TimeLabel_Load(object sender, EventArgs e)
{

}
</script>

Can have web form with C# and another with VB in same website, but not same web application project.

ASPX Page Anatomy

Page contains user interface layout information, code that executes on server and directives to connect layout with the code and tell ASP how to process page. Typical page contains three sections:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherites="_Defaul" %>

Creating ASPX Page

  1. With website open in Visual Studio click Website | Add New Item
  2. In Add New Item dialogue from Installed Templates list select programming language for web form.
  3. Provide a name for the web form
  4. To use code-behind ensure "Place code in separate file" is checked
  5. Can choose to select a master page on which to base look of web form
  6. Click Add, if prompted select master page, then OK.

Website compilation

Can just copy files from development machine to web server. First time a user opens web page ASP.NET compiles application into assembly. This can take a few seconds, slowing down any requests made before ASP.NET has finished compilation.

Dynamic compilation has pros and cons:

Visual studio allows a website to be pre-compiled. Only layout code and associated assemblies deployed to web server.

Using assemblies

If use custom class for multiple applications can compile it into separate assembly. Can reference this assembly from Visual Studio projects via the Add Reference dialogue.

When referencing assembly can choose to store copy with every ASP.NET website, or place a single copy into the GAC. Usually easier to manage assemblies and deploy websites when all assemblies stored within ASP.NET website.

Downloads