Skip to main content

Using Contensis with .Net Razor Pages

Log in to add to favourites

This step-by-step guide will take you through getting your entries from Contensis and displaying them using the Delivery API and a simple .Net Razor Pages app.

โœ”๏ธ Prerequisites

  • Git
  • Basic command line interface knowledge
  • A GitHub account (New to GitHub? Sign up here)
  • Either Visual Studio or VS Code

Visual Studio

VS Code

 

We'll start by setting up our demo project, which is a simple .NET blog application. This app will pull in data from our demo Leif project using Contensis.

Fork the Razor Pages Leif example ๐Ÿด

First, we need to fork the Razor Pages Leif example on GitHub to create our own copy of the repo. To do this:

  1. Log into your GitHub account.
  2. Go to https://github.com/contensis/razor-page-leif-example and fork the repo.

Cloning the repository ๐Ÿค

Now we have our own fork of the repository, we need to clone it to create a copy of the fork on our local machine. There are two ways to do this โ€“ using SSH or HTTPS. If you have already set up SSH keys for your GitHub account, then it's easier to use SSH. If you haven't set up SSH keys, you can use HTTPS instead.

Clone the repository using SSH

  1. First, open up a terminal and cd into your local development folder.
  2. Now clone the repository using SSH with the following command:
Bash
git clone git@github.com:{your-username}/razor-page-leif-example.git
  1. Then navigate to the project directory:
Bash
cd razor-page-leif-example
cd RazorPageLeifExample

Clone the repository using HTTPS

If you haven't set up SSH keys for your GitHub account, you can use HTTPS instead of SSH to clone repositories. Use the following steps to configure Git to use HTTPS.

  1. Depending on your operating system, open Terminal (on macOS and Linux) or Command Prompt (on Windows).
  2. Enter the following commands to configure Git with your GitHub credentials:
Bash
git config --global user.name "Your GitHub Username"
git config --global user.email "your.email@example.com"
  1. Now enter the following command to clone the repository using HTTPS:
Bash
git clone https://github.com/{your-username}/razor-page-leif-example.git
  1. Then navigate to the project directory:
Bash
cd razor-page-leif-example
cd RazorPageLeifExample

Running the Application ๐Ÿฅณ

Now we've successfully cloned the repo and are in the correct folder, we can run the app locally on our machine. To run your app locally, enter the following command in your terminal:

Bash
dotnet watch

This will start the server. You can then access the application by visiting http://localhost:3000 in your web browser.
The application should look something like this:

If you get any errors, make sure the following are installed on your machine:

Using your own content ๐Ÿ“š

At the moment the application is using data from our public demo environment. To start using your own data, you'll need to connect your own Contensis environment. Follow these steps to connect a free trial environment:

  1. Create an account with Contensis or log in.
  2. Navigate to your account dashboard and launch your free trial environment. (If you don't have a free trial, you can request one here).
  3. The first thing we need to do is update the CONTENSIS_CLIENT_ID and CONTENSIS_CLIENT_SECRET in the .env file with credentials from our own API key. Go to Settings > API Keys in your Contensis project.
  4. Press the New API Key button in the top-right corner to create a new API key.
  5. Copy the CONTENSIS_CLIENT_ID and CONTENSIS_CLIENT_SECRET from your API key into the .env file.
  1. You now need to replace the PROJECT_API_ID in the .env file with the API ID from your own project. In your Contensis project, go to Settings > General and copy the API Name field.
  1. Open the .env file in your IDE again and replace the PROJECT_API_ID with the API ID from your Contensis project.
  2. Finally, determine your ALIAS by examining your CMS URL, for instance, cms-leif.cloud.contensis.com. Extract the part that follows cms-. In this example, the ALIAS would be 'leif'.

That's it, you're all set up! ๐Ÿ‘

Still need help?

If you still need help after reading this article, don't hesitate to reach out to the Contensis community on Slack or raise a support ticket to get help from our team.
New support request