ASP.NET Web Development with .NET Framework

This section provides comprehensive documentation on building web applications and services using ASP.NET within the .NET Framework. ASP.NET is a powerful and flexible open-source framework for building modern, cloud-based, internet-connected applications.

Key ASP.NET Technologies

ASP.NET Overview

ASP.NET is a server-side web application framework designed for developing dynamic websites, web applications, and web services. It runs on the .NET Common Language Runtime (CLR), enabling developers to use a wide range of .NET languages like C# and Visual Basic to build applications.

It offers a robust set of features, including:

Web Forms

ASP.NET Web Forms is an event-driven programming model that makes it easier to build dynamic pages. It uses a server-side control model where developers drag and drop controls onto a page, and then write event handlers to respond to user actions. This model abstracts away much of the underlying HTTP complexity.

Tip: Web Forms is ideal for developers familiar with traditional Windows application development paradigms due to its event-driven nature.

A simple Web Forms page structure might look like this:

<!DOCTYPE html>
<html>
<head>
    <title>My Web Form</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="lblMessage" runat="server" Text="Hello, World!" />
        <asp:Button ID="btnClick" runat="server" Text="Click Me" OnClick="btnClick_Click" />
    </form>
</body>
</html>

And the code-behind:

// C# Code-Behind
public partial class MyPage : System.Web.UI.Page
{
    protected void btnClick_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Button was clicked!";
    }
}

Model-View-Controller (MVC)

ASP.NET MVC is a design pattern that separates concerns in an application. It is a lightweight framework that provides a fine-grained control over HTML, CSS, and JavaScript. It consists of three core components:

Note: MVC is generally preferred for applications requiring greater control over the rendered output and for promoting testability.

An MVC controller action:

// C# Controller Action
public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        return View();
    }
}

Web API

ASP.NET Web API is a framework for building HTTP services. These services can be accessed from a wide variety of clients, including browsers, mobile devices, and other server-side applications. Web API is a platform that allows you to build RESTful HTTP services on top of the .NET Framework.

It leverages the HTTP protocol and its features, such as URIs, request/response headers, and content negotiation.

// C# Web API Controller
public class ProductsController : ApiController
{
    public IEnumerable<Product> GetAllProducts()
    {
        // Logic to retrieve products from a data source
        return new Product[] { new Product { Id = 1, Name = "Widget" } };
    }

    public Product GetProduct(int id)
    {
        // Logic to retrieve a specific product
        return new Product { Id = id, Name = "Specific Widget" };
    }
}

Routing

Routing is the process of mapping incoming browser requests to the appropriate handler in your web application. ASP.NET provides sophisticated routing capabilities for both Web Forms and MVC applications, allowing you to define clean, SEO-friendly URLs.

Data Access

ASP.NET integrates seamlessly with various data access technologies within the .NET Framework, including:

Further Reading