ASP.NET Tutorials

Dot Net Perls

Array Collections File String Windows VB.NET Algorithm ASP.NET Cast Class Compression Convert Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method .NET Number Regex Sort StringBuilder Struct Switch Time Value

ASP.NET web programming framework

The ASP.NET runtime generates and serves web pages in cooperation with the IIS web server. It provides several ways to render web pages and send them to browsers. One of its strengths is its versatility. We explore ASP.NET. We begin with its core features and continue with web forms.

ASHX

Class conceptual image

While many websites use ASPX files, which represent pages, you can also use ASHX files, which are simple handlers. None of the overhead associated with the Page class is present here. We reveal more information about the ASHX format.

ASHX Handler Tutorial

Responses

With ASP.NET, you need to receive the request from the web browser over the network, and then generate a response to send back. You can use the BinaryWrite, Write, or WriteFile methods for this.

Response.Write Response.WriteFile Response.BinaryWrite Image Output CompleteRequest Method

File handling

How can you handle files on the local disk on an ASP.NET website in a portable way? We show one way you can do this, in an example that implements a logging mechanism that records every visit to a website. Its performance is not optimal.

File Handling Tutorial

HTTP headers

Hypertext transfer protocol (HTTP)

HTTP headers are an instrumental part of the web: client software sends requests in HTTP header format, and your web server must respond with the appropriate HTTP response headers and the response itself. We describe how to use HTTP headers in ASP.NET.

ContentType Redirect RedirectPermanent Request.Headers Get IP Address (UserHostAddress) Localhost Method ServerVariables

HttpWorkerRequest. One way you can improve the performance of your ASP.NET programs is with the HttpWorkerRequest class. This gives you a lower-level mechanism to access important data, and also to output certain headers and data.

HttpWorkerRequest

Trace

Tracing is an easy way to build diagnostics and timings into your ASP.NET website. It is also easy to disable in case you need more performance from your site after development.

Trace Examples and Tips

Rewrite paths

Often, the path received from the client on the network is not the same as the path on the local server. In ASP.NET, you can dynamically rewrite paths such that the correct server path is always used for external requests.

RewritePath Method urlMappings for Redirects Server.Transfer Method Global.asax TipPath type

Server paths. The ASP.NET environment introduces some complexity to path handling in the C# language because it uses relative web-style paths called URIs. We shed light on how you can use paths and URIs in ASP.NET.

MapPath PhysicalApplicationPath

QueryString

When you access an ASP.NET website with a query string in the URL, such as page.aspx?test=1, the QueryString collection may be used in the C# language to access the "test" value. We cover the QueryString collection with a fair level of completeness.

QueryString UsageCache illustration

Caching

Here, we focus on how to improve performance of websites using caches, covering different issues related to caching in ASP.NET.

Cache Examples Expiration Time Output Cache Tutorial SetCacheability Issue

Caching means saving a few values in such a way that you can retrieve the most commonly used values more easily than the less commonly used values.
McConnell, p. 628

Caching specific values. In addition to caching entire pages, you can cache certain values that those pages require. We show how you can cache settings in ASP.NET, reducing the time required to fetch values.

appSettings Cache

HttpContext

The HttpContext type is essential to ASP.NET development as it provides functionality related to the HTTP protocol and the current request and response on the server. We dive into the HttpContext type.

HttpContext Request Property HttpContext Timestamp Property IsDebuggingEnabled Property

Configuration

Programming tip

Configuring ASP.NET websites is one of the most frustrating part of developing them. These examples span several configuration topics: you can change error outputs, how the site is compiled, what modules are loaded, and the settings used in the website.

customErrors Tutorial HTTP Pipeline Precompiled Site Remove httpModules Web Application Project appSettings Example X-Aspnet-Version Header

Search queries

Google search engine

We describe an algorithm for parsing Google search queries. Your website will receive these in the HTTP referer header, and you can use C# and ASP.NET to parse the queries.

Google Search Query

AJAX

With AJAX, you can update pages on the client side without needing to change the entire page. You can simply update parts of pages, as we show in this example that ties the Page_Load event to the AJAX technique.

Page_Load Event and AJAX

Global variables

There are several ways you can use global variables in ASP.NET websites. Typically, the easiest way is to use a public static field, but you can also use public methods or properties as accessors. We demonstrate some ways to use global variables in ASP.NET.

Global Variables Example

XML

Extensible markup language (XML)

We show how to use the XmlWriter type on the OutputStream in an ASP.NET project: this enables you to write XML imperatively to a webpage. We describe this approach in more depth.

OutputStream

Web forms

Page illustration

Web forms enable ASP.NET developers to quickly create web pages. This feature is object-oriented and can help you keep your web page generation code easier to maintain and improve. These examples show how the web forms markup (stored in *.aspx pages) can be controlled with the C# code (stored in *.cs files).

Fragment of web forms markup

<asp:label runat="server" id="Perls"></asp:label>

Page_Load event handler for web forms [C#]

using System;
using System.Web.UI;

namespace WebApplication1
{
    public partial class WebForm1 : Page
    {
	protected void Page_Load(object sender, EventArgs e)
	{
	    Perls.Text = "Welcome";
	}
    }
}

Contents of rendered page

Welcome

Overview. When a user visits the page with a web browser, the C# code is executed and acts upon the markup; the rendered page is the result of that interaction.

Master pages

Master Page dialog in Visual Studio

There is some information about master pages and their usage as well. Master pages are still in wide usage and can help separate the template from the more specific content sections.

Master Page Master Page SEO Master Page Drawbacks

LiteralControl

You can insert plain text into a website using the asp:Literal markup or the LiteralControl element in C# code. We describe how you can use literals in ASP.NET.

asp:Literal LiteralControl

Elements

The HtmlMeta type enables you to represent a metadata element for HTML inside C# code. You can use the HtmlMeta to add important information to the metadata of your webpages. You can also use the HtmlLink element.

HtmlMeta HtmlLink

ID

ID attribute

It is possible to remove the generated ID attribute from your web form projects in ASP.NET. You can simply set the ID to null to do this. This yields small reductions in page size.

Remove ID From Control

Visible

Property (Icon copyright Microsoft)

The Visible property is one of the more useful ones for using web forms because it enables you to use a template and remove or add elements from it without creating them programmatically. We describe the Visible property in more detail.

Visible

SiteMap

Web Forms also provides the Site Map file type, which you can use in your project to store index information about your web site.

SiteMap Example

IIS

These features are not strictly ASP.NET features—they are markup features for IIS pages. Server side comments can be used to remove text from pages before they are sent over the network, reducing page sizes. Files can also be included through server-side includes.

Server-Side Comments SSI Server-Side Includes

Summary

The C# programming language

We explored the ASP.NET web programming framework, covering both general topics as well as web forms, in the context of the C# programming language. Just as the C# language can be used in desktop or console programs, so too it is effective on the server for generating web pages.