Skip to content

Sample .NET MVC web application using the Kontent.ai Delivery .NET SDK to retrieve content

License

Notifications You must be signed in to change notification settings

kontent-ai/sample-app-net

Repository files navigation

Important

This sample app relies on the Dancing Goat project, which is no longer available for creation in Kontent.ai.

If you still wish to use this project, you can import the Dancing Goat project using the Data Ops tool by following the instructions provided in our Dancing Goat repository.

Kontent.ai sample ASP.NET Core MVC web application

Build & Test codecov Stack Overflow Discord

This is a sample ASP.NET Core MVC app that uses the Kontent.ai Delivery .NET SDK to retrieve content from Kontent.ai.

Getting started

To run the app:

  1. Clone the app repository

  2. Run npm install && npm run build in the DancingGoat directory to build CSS files for the project (node.js must be installed before running this command)

  3. Open the DancingGoat.sln solution file and run the app

  4. Follow the setup wizard to setup your project or adjust the \DancingGoat\appsettings.json file:

    {
      // ...
      "DeliveryOptions": {
        "ProjectId": "YOUR_PROJECT_ID"
      }
      // ...
    }

Follow the step-by-step tutorial for even more details.

Features

Model generation

With the help of Kontent.ai model generator you can generate models from your project. This process will create classes that you can work with within your codebase. To run the generation, use the Powershell script ./DancingGoat/Tools/GenerateModels.ps1 or Bash script ./DancingGoat/Tools/GenerateModels.sh.

Be aware that by default, the scripts downloads the models from the default Dancing Goat project. If you want to download models from your custom project, change the project id inside the script file.

Edit mode & preview

Content contributors sometimes need to fix errors or typos right when they see them on the website. The sample app allows users to navigate from a piece of content on the site straight to the corresponding content item or element in Kontent.ai.

To see Edit mode in action:

  1. In your Kontent.ai project navigate to Project Settings -> API keys to get the Preview API key.

  2. Enable Delivery Preview API by adding the key to the \DancingGoat\appsettings.json file:

    {
      // ...
      "DeliveryOptions": {
        "UsePreviewApi": true,
        "PreviewApiKey": "YOUR_DELIVERY_PREVIEW_API_KEY"
      }
      // ...
    }
    • Delivery Preview API: change the key named PreviewApiKey in the DeliveryOptions section, and use the Delivery Preview API key as its value. To enable calls over the Delivery Preview API, you also need to change the value to true of the key named UsePreviewApi.
  3. Run the app.

  4. Navigate to the About us section.

  5. Click the Edit mode switch in the bottom-left corner.

Edit buttons will appear next to each piece of content on the page. To explore how the functionality is implemented, navigate to the TagHelpers folder.

Responsive images

The sample app contains a sample implementation of the img-asset tag helper from the Kontent.Ai.AspNetCore NuGet package. Using the img-asset tag helper, you can easily create an img tag with srcset and sizes attributes. Read more about image transformation API. You can adjust the behaviour in the appsettings.json file.

"ImageTransformationOptions": {
  "ResponsiveWidths": [ 200, 400, 600, 800, 1000, 1200, 1400, 1600, 2000, 4000 ]
},

Localized routing

The app demonstrates the usage of language prefixes (e.g. /en-US/articles) for localizing URLs for SEO purposes. Each language is identified by its codename, in case of this project, it is en-US and es-ES. The used language is obtained from the URL via /DancingGoat/Infrastructure/RouteRequestCultureProvider and set as the culture of the app.

Get involved

Check out the contributing page to see the best places to file issues, start discussions, and begin contributing.