Razor Pages

| Sania Uzair

What are Razor Pages?

ASP.NET Razor Pages is a server-side, page-focused framework that enables constructing dynamic, data-driven web sites with clean separation of concerns. It was first introduced as part of ASP.NET Core and is currently included in.NET 5. Razor Pages, which is part of Microsoft's ASP.NET Core web development framework, offers cross-platform development and can be deployed on Windows, Unix, and Mac operating systems.

Razor syntax:

A Razor content page serves as a starting point for creating HTML. Static HTML, tag helpers that dynamically emit HTML, and C# code are all common components of a content page. According to a set of conventions, or syntax, the C# code is placed within the static content and tag helpers.

All code blocks must be enclosed by @{.... }  brackets. You're assumed to have started writing C# code as soon as you type @. Unless you tell Razor otherwise, everything that follows is assumed to be code:

  1. @{
  2. var numbers = Enumerable.Range(1, 10); //Get numbers from 1 - 10
  3. foreach(var num in numbers){
  4. ...
  5. }
  6. }

Variable values are prefixed with the @ symbol before being turned into HTML:

  1. The time is @DateTime.Now

If you want to render the variable number from the previous example within the loop, you must precede it with a @ sign:

  1. @{
  2. var numbers = Enumerable.Range(1, 10); //Get numbers from 1 - 10
  3. foreach(var number in numbers){
  4. @number
  5. }
  6. }

Typically, statements will be declared in a code block separate from the Razor code that renders them. When used to control rendering, iteration and selection statement keywords (if, for, foreach, switch, while, etc.) do not require curly braces. A simple @ symbol will suffice. The same is true for try... catch.


Razor Pages has the advantage of collecting everything connected to a specific request in one place. It has specific codes behind individual pages and is more organized..

Join us next time, as we continue our journey of learning canvas apps.Click here to learn more about Imperium's Power Apps Services. We hope this information was useful, and we look forward to sharing more insights into the Power Platform world.

Chief Architect, Founder, and CEO - a Microsoft recognized Power Platform solution architect.

About The Blog

Stay updated with what is happening in the Microsoft Business Applications world and initiatives Imperium is taking to ease digital transformation for customers.

More About Us

We provide guidance and strategic oversight to C-Suite and IT Directors for on-going implementations. Feel free to give us a call.

1 331 250 27 17
Send A Message

Ready to Start?

Get a personalized consultation for your project.

Book a Meeting