Chapter 7: WebPages Objects
WebPages Objects (exactly the title in the W3Schools menu: “WebPages Objects”).
This lesson is all about the built-in objects that are always available to you in every .cshtml page. You don’t create them — the framework gives them to you for free. They’re like your “super tools” for talking to the browser, the server, the user, and even other parts of your site.
Think of them as ready-made assistants:
- One reads what the user sent (forms, URLs, cookies…)
- One writes back to the browser
- One helps with server tasks (like mapping paths)
- Others handle data shared across users or per-user sessions
W3Schools says it simply: “Web Pages is much often about Objects.”
The main ones covered in this lesson (and used throughout the tutorial) are:
- Page (the special one for Razor/Web Pages)
- Request
- Response
- Server
- Application
- Session
Today we’ll go deep into each — with real explanations, code examples (adapted from W3Schools + my extra teacher ones), and why/when you use them. Let’s break it down!
1. The Page Object – The “This Page” Helper (Most Unique to Web Pages)
This is not from classic ASP — it’s special to ASP.NET Web Pages / Razor.
The Page object gives you:
- Properties like IsPost, Layout
- Methods like @RenderBody(), @RenderPage(), @RenderSection()
- And the super-useful Page.Title, Page.anything (dynamic bag for sharing data between content page and layout)
Key Page Properties & Methods (from the lesson table)
| Property/Method | What it does | Example Usage |
|---|---|---|
| IsPost | true if page was posted back (form submit) | if (IsPost) { … } |
| Layout | Get/set the layout file path | Layout = “~/Shared/_Layout.cshtml”; |
| Request | Shortcut to the full Request object | Request[“username”] |
| Server | Shortcut to Server object | Server.MapPath(“~/data.txt”) |
| Page (the bag) | Dynamic property bag — share data between page & layout | Page.Title = “Home”; |
| href(…) | Builds safe URLs from ~ paths | <a href=”@Href(“~/About”)”>About</a> |
| RenderBody() | Where main content goes in layout | In _Layout: @RenderBody() |
| RenderPage(“file”) | Insert another .cshtml file | @RenderPage(“_Header.cshtml”) |
| RenderSection(“name”) | Insert named section from content page | @RenderSection(“scripts”, false) |
| Write(obj) | Write HTML-encoded text | Page.Write(“Hello”) (rare) |
| WriteLiteral | Write raw (no encoding) | Useful for scripts sometimes |
Real Example: Using Page Bag to Set Title from Content Page
Home.cshtml (content page)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@{ Layout = "~/Shared/_Layout.cshtml"; // Add your own properties to Page bag Page.Title = "Welcome to Webliance - Home"; Page.Subtitle = "Learning ASP.NET Web Pages in Hyderabad"; Page.ActiveMenu = "home"; } <h1>Hello from Home!</h1> |
Shared/_Layout.cshtml
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html> <head> <title>@Page.Title</title> <!-- ← uses value from content page --> </head> <body> <header> <h2>@Page.Subtitle</h2> <!-- dynamic! --> <nav> <a class="@(Page.ActiveMenu == "home" ? "active" : "")" href="/">Home</a> <!-- ... other menu items --> </nav> </header> @RenderBody() </body> </html> |
→ Every content page sets Page.Title, Page.ActiveMenu, etc. → layout uses them automatically. Super clean!
2. Request Object – Get Data from User/Browser
This is how you read everything the client sent:
- Form fields → Request[“fieldname”] or Request.Form[“…”]
- URL query string → Request.QueryString[“id”] or Request[“id”]
- Cookies, headers, server variables…
Common in forms (from previous lesson):
|
0 1 2 3 4 5 6 7 8 9 10 |
if (IsPost) { var name = Request["username"]; // from <input name="username"> var age = Request["age"].AsInt(0); // convert to int safely var ip = Request.UserHostAddress; // visitor's IP } |
3. Response Object – Send Stuff Back to Browser
You rarely call it directly in Razor (because HTML just flows), but useful for:
- Redirects
- Set cookies
- Change headers
- End response early
Examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@{ if (!WebSecurity.IsAuthenticated) { Response.Redirect("~/Account/Login"); // Response.End(); // optional - stop here } Response.Cookies["LastVisit"].Value = DateTime.Now.ToString(); Response.Cookies["LastVisit"].Expires = DateTime.Now.AddDays(30); } |
4. Server Object – Server-Side Helpers
Great for paths, encoding, etc.
Common ones:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
@{ var physPath = Server.MapPath("~/App_Data/data.txt"); // C:\...\data.txt var safeHtml = Server.HtmlEncode("<script>alert()</script>"); // &lt;script&gt;... var urlEncoded = Server.UrlEncode("hello world"); // hello+world } |
5. Application Object – Shared Across ALL Users
Like a global variable bucket (shared by everyone, careful with concurrency!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@{ // In _AppStart.cshtml or anywhere if (Application["VisitorCount"] == null) { Application["VisitorCount"] = 0; } Application.Lock(); // important for multi-user safety Application["VisitorCount"] = (int)Application["VisitorCount"] + 1; Application.UnLock(); } |
Then anywhere: <p>Visitors so far: @Application[“VisitorCount”]</p>
6. Session Object – Per-User Data
Stores info for one user across pages (until browser closes or timeout).
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
@{ // On login page Session["UserName"] = "Rahul"; Session["Role"] = "Admin"; // Timeout in minutes Session.Timeout = 30; } |
Then on any page:
|
0 1 2 3 4 5 6 7 8 9 10 |
@if (Session["UserName"] != null) { <p>Welcome back, @Session["UserName"]!</p> } else { Response.Redirect("~/Login"); } |
Quick Summary Table – Your Cheat Sheet
| Object | Scope | Main Use Case | Example Code Snippet |
|---|---|---|---|
| Page | Current page + layout | Layout integration, IsPost, dynamic data | Page.Title = “About”; |
| Request | Incoming request | Read forms, querystring, cookies, IP | Request[“email”] |
| Response | Outgoing response | Redirect, set cookies, headers | Response.Redirect(“~/Home”) |
| Server | Server utilities | Map paths, encode/decode | Server.MapPath(“~/images”) |
| Application | All users, whole app | Global counters, shared config | Application[“SiteName”] |
| Session | One user session | Login state, shopping cart | Session[“CartItems”] = list; |
Teacher Closing Thoughts
These objects are the foundation — almost every real Web Pages site uses most of them daily. In modern ASP.NET Core Razor Pages:
- Page → becomes the PageModel class
- Request/Response → HttpContext.Request / .Response
- Session → still there (but less used, prefer cookies/temp data)
But the ideas are identical — master them here!
Next questions?
- Want a full example combining Request + Session + Page for a login system?
- Deep dive into one object (like full Request collections)?
- Or ready for WebPages Files (reading/writing files)?
Just say — we’re making great progress in Hyderabad! Keep coding strong, Webliance! 🚀🇮🇳
