Chapter 17: Web Pages Classes
Web Pages Classes
This page is not about teaching you how to write your own custom classes in C# (like public class Product { public string Name { get; set; } } — that’s something you can do later in the App_Code folder if you want real OOP).
Instead, it’s a pure reference/cheat sheet — a single, long table that lists dozens of the most frequently used built-in helpers, methods, properties, and extension methods that are automatically available inside every .cshtml (or .vbhtml) file.
Think of it as your pocket reference card or quick-lookup index after you’ve learned the main concepts (Razor syntax, forms, databases, helpers, security, etc.). W3Schools puts it at the end so you can come back here whenever you forget “how do I safely convert a string to int?” or “how do I share a title between page and layout?”
It’s titled “ASP.NET Classes Reference”, but many entries are actually static methods, extension methods on strings, properties of the current page, or Razor-specific helpers — not traditional OOP classes.
Why This Page Matters (Teacher Perspective)
- You don’t need to using anything or create objects — everything here is ready to use.
- Covers the “magic” that makes Web Pages feel simple compared to full MVC or Web Forms.
- Many items appear in almost every real page you write (especially IsPost, AsInt(), Page.Title, Href(), RenderBody()).
- It’s the summary of the entire WP Tutorial track — once you know this list, you’re dangerous!
The page shows a big table with two columns:
Method / Property Description
Below I’ll walk through every major group from that table (as of 2026 — the content hasn’t changed much since ~2012), with detailed explanations + practical code examples (mostly C# — VB is very similar except for syntax like Dim vs var, End If vs }).
1. String Conversion Helpers (The Most Used Ones – Safety First!)
These are extension methods you call directly on any string (usually from Request[“something”]).
They never throw exceptions if conversion fails — they return a default value instead (very forgiving for user input).
| Method | Description | Real Example (C#) |
|---|---|---|
| AsBool() / AsBool(default) | Converts to bool. Returns false (or your default) if invalid. | bool newsletter = Request[“subscribe”].AsBool(false); |
| AsDateTime() / AsDateTime(default) | To DateTime. Returns DateTime.MinValue or your default if fails. | DateTime eventDate = Request[“date”].AsDateTime(); |
| AsDecimal() / AsDecimal(default) | To decimal (great for money/prices). | decimal price = Request[“amount”].AsDecimal(0m); |
| AsFloat() / AsFloat(default) | To float. | float rating = Request[“score”].AsFloat(); |
| AsInt() / AsInt(default) | To int — the #1 most used! | int quantity = Request[“qty”].AsInt(1); |
Why always use these? int qty = Convert.ToInt32(Request[“qty”]); → crashes if user types “abc”. int qty = Request[“qty”].AsInt(0); → safe, defaults to 0.
2. Type Checking Helpers (Before Converting)
| Method | Returns true if… | Example |
|---|---|---|
| IsBool() | String can be converted to bool | if (Request[“active”].IsBool()) { … } |
| IsDateTime() | Valid date | if (Request[“dob”].IsDateTime()) { var d = Request[“dob”].AsDateTime(); } |
| IsDecimal(), IsFloat(), IsInt() | Same idea for numbers | — |
3. URL & Path Helpers
| Method | What it does | Example |
|---|---|---|
| Href(path, [param1, param2, …]) | Builds clean URL from ~ virtual path + route segments | <a href=”@Href(“~/Products”, “Details”, 123)”>View</a> → /Products/Details/123 |
| Server.MapPath(virtualPath) | Virtual (~) → physical disk path (for File.ReadAllLines, etc.) | string path = Server.MapPath(“~/App_Data/users.txt”); |
4. HTML Encoding / Raw Output
| Method | Purpose | Example |
|---|---|---|
| Html.Raw(string) | Outputs HTML without escaping (dangerous — XSS risk if user input!) | @Html.Raw(“<div class=’alert’>Welcome!</div>”) |
| Server.HtmlEncode(text) | Escapes < > & → safe for output | <p>@Server.HtmlEncode(userComment)</p> |
| Server.HtmlDecode(encoded) | Reverse — rarely needed | — |
5. Page & Layout Core (The Heart of Web Pages)
These are properties/methods of the current page context.
| Name | Type / Usage | Example |
|---|---|---|
| IsPost | Property: bool — true on form POST | if (IsPost) { process form } |
| Layout | Property: set layout path | Layout = “~/Shared/_Layout.cshtml”; |
| Page / PageData | Dynamic dictionary — share data page ↔ layout/partial | Content page: Page.Title = “About Us”; Page.Active = “about”; Layout: <title>@Page.Title</title> <a class=”@(Page.Active == “about” ? “active” : “”)”>About</a> |
| @RenderBody() | Method: in layout — main content placeholder | In _Layout: <main>@RenderBody()</main> |
| @RenderPage(“path” [, data]) | Method: insert another .cshtml file | @RenderPage(“_Sidebar.cshtml”, new { Categories = cats }) |
| @RenderSection(“name”, required: false) | Method: in layout — render named section from content page | Layout: @RenderSection(“scripts”, required: false) Content: @section scripts { <script>alert(‘hi’)</script> } |
| @section name { … } | Syntax: in content page — define block for layout | See above |
6. Other Common Ones
- Request, Response, Server, Session, Application → same as classic ASP (covered in WebPages Objects lesson)
- WebSecurity, WebMail, Database, WebGrid, Chart → the big helpers (covered in their own lessons)
Final Teacher Summary
“ASP.NET Web Pages – Classes” = not about custom classes — it’s a reference list of:
- Safe converters (AsInt(), AsDateTime()…)
- Page/layout magic (Page.Title, RenderBody(), RenderSection()…)
- URL/path tools (Href(), MapPath()…)
- HTML safety (Html.Raw, Server.HtmlEncode…)
- And quick checks (IsInt(), IsPost…)
Bookmark this page — when you build real sites, you’ll open it every 10 minutes.
Got it now? 😊
If you want:
- How to create your own custom class in App_Code (real OOP example)?
- Full mini-project using many of these “classes”/helpers together?
- Or jump to modern ASP.NET Core Razor Pages (where PageModel replaces a lot of this)?
Just tell me — you’ve conquered the whole W3Schools Web Pages track like a champion from Hyderabad! 🚀🇮🇳
