Creating Websites with ASP.NET
Creating Website
Visual Studio projects allow definition of new site based on how intend to access its content.
- File System - stores all files for site in directory of your choosing. When debugging Visual Studio uses lightweight ASP.NET development server included with Visual Studio. Great for when want to run and debug locally, but do not want to run a local copy of IIS.
- FTP - used to manage files on a remote server. Typically used when website hosted on remote computer.
- HTTP - used when working with site hosted by IIS (either locally or remotely). The website must be at the root of the IIS server, or in a virtual directory configured as an application. The IIS server will need the WebDAV publishing role service or Front Page Server extensions.
Steps...
- Use File | New | Web Site
- Select website type, location and programming language
- Select target .NET framework (2, 3, 3.5 or 4) - preferably 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:
- Create MVC applications
- List of files stored in project file rather than relying on folder structure
- Cannot mix VB and C#
- Cannot edit code without stopping a debugging session
- Can set-up project dependencies between multiple web projects
- Application must be compiled before deployment - prevents testing as page if another does not compile
- Do not have to store source code on server-side
- Can control assembly name and version
- Cannot edit individual files after deployment without recompiling
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...
- Install .NET 4 on server
- Install IIS 7.5 with ASP.NET role
- Open IIS manager. Create new website that uses ASP.NET 4 application pool
- Grant web developer user account NTFS write permissions to website folder
- Share website folder, grant web developer accounts Change share permissions.
On development machine...
- Map network drive to shared folder
- 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...
- Follow previous steps, but do not share website folder. Instead configure WebDAV as follows...
- Add WebDAV Publishing role to IIS
- In IIS Manager add WebDav Authoring Role to grant user accounts access to Read, Write and Source for website
- Enable WebDAV on server-side
On development machine...
- Map network drive to websites URL
- 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:
- Internet Information Services | Web Management Tools | IIS 6 Management Compatibility | IIS 6 Metabase And IIS 6 Configuration Compatibility
- Internet Information Services | World Web Services | Application Development Features | ASP.NET
To create website using IIS:
- Launch Visual Studio as an Administrator
- Create a new website
- Select website type and language
- Choose HTTP as Web Location
- Set Local IIS as location for site
- 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...
- Configure server as in File System Based Website
- Install FTP server role service on server hosting website
On client...
- Use Visual Studio to create new website
- From Web Site Type drop-down select FTP
- 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:
- List of projects making up solution
- Default language
- Project dependencies
- Source control information
- List of available add-ins
.suo is binary file of user settings related to IDE. Contains
- Task list
- Debugger break points
- Window locations
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 directives - set up environment and specify how page is processed. Indicate associated code file, development language, transaction, etc. e.g.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherites="_Defaul" %>
- Code - Contains code to handle events that execute on server based on ASP.NET page processing.
- Page layout - Written using HTML that include body, mark-up and style information.
Creating ASPX Page
- With website open in Visual Studio click Website | Add New Item
- In Add New Item dialogue from Installed Templates list select programming language for web form.
- Provide a name for the web form
- To use code-behind ensure "Place code in separate file" is checked
- Can choose to select a master page on which to base look of web form
- 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:
- Pro - Changes to a site file causes recompilation => easy deployment
- Pro - Entire application does not need to be recompiled when change made to single page - great for large sites
- Pro - pages containing compilation errors do not prevent other pages from loading
- Con - first request for a page takes a compilation performance hit
- Con - Source code must be deployed to server
Visual studio allows a website to be pre-compiled. Only layout code and associated assemblies deployed to web server.
- Pro - Verifies that all pages and dependencies can be compiled
- Pro - Performance for first user request is improved
- Con - Difficult to deploy and manage as only copy necessary files versus copying all files to 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.