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)

HTML

Shared/_Layout.cshtml

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):

HTML

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:

HTML

4. Server Object – Server-Side Helpers

Great for paths, encoding, etc.

Common ones:

HTML

5. Application Object – Shared Across ALL Users

Like a global variable bucket (shared by everyone, careful with concurrency!)

HTML

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).

HTML

Then on any page:

HTML

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! 🚀🇮🇳

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *