Razor pages cheat sheet
Bullet Points: Connecting web app with database using
[Link]:
- C# code is case sensitive
- C# comment: 1. open a connection
2. form a query
@* your comment *@
3. form a SqlCommand
- all new razor pages must be added inside 4. execute the command
the Pages folder 5. catch any exceptions
- _Layout.cshtml is a partial view which 6. close the connection
encapsulates all razor pages inside the
SqlCommand components and different methods:
Pages folder
- Handler methods must be public. They
can return either void or IActionResult
- An html form must have a method
attribute, and it must be equal to post.
<form method="post">
- Add an asp-page-handler attribute to the
form if there are multiple OnPost() Connection String Format:
methods in one page
Data Source= <yourDeviceName>; Initial
- Handler methods must use the following
Catalog= <targetDatabase>; Integrated
naming convention:
Security= True; TrustServerCertificate =
True;
To pass parameters from the frontend to the
backend:
Frontend:
Tag Helpers:
<input type="text" asp-for="@[Link]">
1. asp-route <input type="text" asp-for="@[Link]">
2. asp-route-id
3. asp-page-handler Backend:
4. asp-for
5. asp-page [BindProperty]
public string Name { get; set; }
6. asp-validation-for
[BindProperty]
Structure of handler methods: public string Email { get; set; }
public void OnGet(){} Model Validation:
public void OnPost(){}
[Link]
- Required package:
In Visual Studio> Tools> NuGet Package Manager>
Manage NuGet Packages for Solution> Search for Sessions:
[Link]> Install
Setup:
Model Validation package: [Link]();
[Link]();
- [Link]
Usage:
[Link]("username","value");
[Link]("username");
[Link]("username");