
Webpages Objects in ASP
In ASP.NET Web Pages (Razor), there are several built-in objects that you can use to work with the web environment — such as requests, responses, cookies, sessions, files, and more.
These objects are similar to classic ASP and ASP.NET but tailored to the Razor syntax and simplified for ease of use.
? Common Web Pages Objects in ASP.NET
Here’s a list of the most-used objects in Web Pages and what they do:
1. Request
Used to access information sent by the browser to the server.
@{ var name = Request["name"]; // From query string or form var userAgent = Request.UserAgent;}
2. Response
Used to send output or instructions back to the browser.
@{ Response.ContentType = "text/plain"; Response.Write("Hello, world!"); Response.End(); // Stop page processing}
3. Session
Stores user-specific data between page requests.
@{ Session["username"] = "john"; var name = Session["username"];}
4. Application
Stores data that is shared across all users of the app.
@{ Application["siteName"] = "My Website"; var site = Application["siteName"];}
5. Server
Provides server-side functions like URL encoding or error handling.
@{ var encoded = Server.HtmlEncode("<h1>Hello</h1>");}
6. File
, Directory
, FileSystem
Used to read/write files and folders on the server.
@{ var text = File.ReadAllText(Server.MapPath("~/data.txt")); File.WriteAllText(Server.MapPath("~/output.txt"), "Hello!");}
7. WebSecurity
, Crypto
, AntiForgery
Used for authentication and security.
@{ var hashed = Crypto.HashPassword("mypassword");}
8. Model
When using data binding with forms or data objects:
@Model.Name
9. Validation
Used to perform validation checks on user input.
@{ if(!Validation.IsValid()) { foreach (var error in Validation.GetErrors()) { <p>@error.Message</p> } }}
10. Url
Helps generate URLs dynamically.
@Url.Content("~/images/logo.png")
? Summary Table
Object | Purpose |
---|---|
Request | Get query string, form, headers |
Response | Set output or headers |
Session | Store per-user data |
Application | Store global shared data |
Server | HTML encoding, map paths |
File / Directory | Work with files and folders |
WebSecurity | Login and user management |
Validation | Handle form validation |
Url | Generate relative paths |
Let me know if you’d like examples of any specific object in use — like uploading files with Request.Files
, or login with WebSecurity
!