0% found this document useful (0 votes)
25 views1 page

Razor Pages Cheat Sheet

This cheat sheet provides essential guidelines for connecting a Razor Pages web application with a database using ADO.NET, including case sensitivity in C#, comment syntax, and the structure of handler methods. It outlines the necessary steps for database interaction, such as opening a connection, forming a query, and executing commands, along with the proper use of connection strings and model validation. Additionally, it covers the use of Tag Helpers for passing parameters between the frontend and backend, and session management in ASP.NET Core.

Uploaded by

s-mohamed.farouk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views1 page

Razor Pages Cheat Sheet

This cheat sheet provides essential guidelines for connecting a Razor Pages web application with a database using ADO.NET, including case sensitivity in C#, comment syntax, and the structure of handler methods. It outlines the necessary steps for database interaction, such as opening a connection, forming a query, and executing commands, along with the proper use of connection strings and model validation. Additionally, it covers the use of Tag Helpers for passing parameters between the frontend and backend, and session management in ASP.NET Core.

Uploaded by

s-mohamed.farouk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Razor pages cheat sheet

Bullet Points: Connecting web app with database using


Ado.net:
- 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="@Model.Name">
1. asp-route <input type="text" asp-for="@Model.Email">
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(){}

ADO.net

- Required package:

In Visual Studio> Tools> NuGet Package Manager>


Manage NuGet Packages for Solution> Search for Sessions:
Microsoft.Data.SqlClient> Install
Setup:
Model Validation package: builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession();
- System.ComponentModel.DataAnnotations
Usage:
HttpContext.Session.SetString("username","value");
HttpContext.Session.GetString("username");
HttpContext.Session.Remove("username");

You might also like