ASP.NET Web Pages (Razor)

ASP.NET Web Pages with Razor syntax provide a lightweight way to build dynamic, data-driven websites using C#. It’s beginner-friendly and great for rapid development.


What Are ASP.NET Web Pages and Razor?

ASP.NET Web Pages is a simplified web framework built around Razor syntax β€” a clean, HTML-friendly way to embed C# logic in your markup.

<h1>Welcome</h1>
<p>The time is: 4/3/2025 1:21:10 PM</p>

Using Layout Pages

You can define a shared layout for consistent structure across pages:

// _Layout.cshtml
<html>
<body>
    <header>My Site</header>
    @RenderBody()
</body>
</html>

Working with Forms

Razor makes handling form submissions easy:

<form method="post">
    <input name="username" />
    <button>Submit</button>
</form>

@{
    var user = Request["username"];
    if (!string.IsNullOrEmpty(user))
    {
        <p>Hello, @user!</p>
    }
}

Connecting to a Database

Use Database.Open for simple queries:

var db = Database.Open("MyDB");
var rows = db.Query("SELECT * FROM Users");

Using Helpers and WebGrid

Razor provides helpers to simplify common tasks like sending email, rendering images, or creating a data grid:

@WebGrid.GetHtml(source: Model.Users)

Security and Publishing

  • Use WebSecurity for simple login and registration
  • Deploy using FTP, Visual Studio publish wizard, or Azure App Service

ASP.NET Web Pages is a great way to start web development with .NET. Next, we’ll dive into ASP.NET MVC and explore a more structured and scalable web application architecture.