Getting Started with ASP.NET Core Carousel Control

29 Sep 20226 minutes to read

This section briefly explains about how to include ASP.NET Core Carousel control in your ASP.NET Core application using Visual Studio.

Prerequisites

System requirements for ASP.NET Core controls

Create ASP.NET Core web application with Razor pages

Install ASP.NET Core package in the application

Syncfusion ASP.NET Core controls are available in nuget.org. Refer to NuGet packages topic to learn more about installing NuGet packages in various OS environments. To add ASP.NET Core controls in the application, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for Syncfusion.EJ2.AspNet.Core and then install it.

The Syncfusion.EJ2.AspNet.Core NuGet package has dependencies, Newtonsoft.Json for JSON serialization and Syncfusion.Licensing for validating Syncfusion license key.

Add Syncfusion ASP.NET Core Tag Helper

Open ~/Views/_ViewImports.cshtml file and import the Syncfusion.EJ2 TagHelper.

@addTagHelper *, Syncfusion.EJ2

Add style sheet

Checkout the Themes topic to learn different ways (CDN, NPM package, and CRG) to refer styles in ASP.NET Core application, and to have the expected appearance for Syncfusion ASP.NET Core controls. Here, the theme is referred using CDN inside the <head> of ~/Pages/Shared/_Layout.cshtml file as follows,

<head>
    ...
    <!-- Syncfusion ASP.NET Core controls styles -->
    <link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/20.3.56/fluent.css" />
</head>

Add script reference

In this getting started walk-through, the required scripts are referred using CDN inside the <head> of ~/Pages/Shared/_Layout.cshtml file as follows,

<head>
    ...
    <!-- Syncfusion ASP.NET Core controls scripts -->
    <script src="https://cdn.syncfusion.com/ej2/20.3.56/dist/ej2.min.js"></script>
</head>

Register Syncfusion Script Manager

Open ~/Pages/Shared/_Layout.cshtml page and register the script manager at the end of `<body>` in the ASP.NET Core application as follows.

<body>
...
    <!-- Syncfusion ASP.NET Core Script Manager -->
    <ejs-scripts></ejs-scripts>
</body>

Now, add the Syncfusion ASP.NET Core Carousel tag helper in ~/Pages/Index.cshtml page.

<div class="col-lg-12 control-section default-carousel-section">
    <div class="e-sample-resize-container carousel-sample">
        <ejs-carousel id="defaultCarousel" cssClass="default-carousel">
            <e-carousel-items>
                <e-carousel-item template="#templateItem1"></e-carousel-item>
                <e-carousel-item template="#templateItem2"></e-carousel-item>
                <e-carousel-item template="#templateItem3"></e-carousel-item>
                <e-carousel-item template="#templateItem4"></e-carousel-item>
                <e-carousel-item template="#templateItem5"></e-carousel-item>
            </e-carousel-items>
        </ejs-carousel>
    </div>
</div>

<style>
.default-carousel-section .carousel-sample {
    margin: 0 auto 2em;
    max-width: 500px;
    height: 300px;
}

.default-carousel .e-carousel-items .e-carousel-item .img-container {
    height: 100%;
}

.default-carousel .e-carousel-items .e-carousel-item .img-caption {
    bottom: 4em;
    color: #fff;
    font-size: 12pt;
    height: 2em;
    position: relative;
    padding: 0.3em 1em;
    text-align: center;
    width: 100%;
}
</style>

<script id="templateItem1" type="text/x-template">
    <figure class="img-container">
        <img src="css/carousel/images/bridge.jpg" alt="bridge" style="height:100%; width: 100%" />
        <figcaption class="img-caption">Golden Gate Bridge, San Francisco</figcaption>
    </figure>
</script>

<script id="templateItem2" type="text/x-template">
    <figure class="img-container">
        <img src="css/carousel/images/trees.jpg" alt="spring_trees" style="height:100%; width: 100%" />
        <figcaption class="img-caption">Spring Flower Trees</figcaption>
    </figure>
</script>

<script id="templateItem3" type="text/x-template">
    <figure class="img-container">
        <img src="css/carousel/images/waterfall.jpg" alt="waterfall" style="height:100%; width: 100%" />
        <figcaption class="img-caption">Oddadalen Waterfalls, Norway</figcaption>
    </figure>
</script>

<script id="templateItem4" type="text/x-template">
    <figure class="img-container">
        <img src="css/carousel/images/sea.jpg" alt="sea" style="height:100%; width: 100%" />
        <figcaption class="img-caption">Anse Source d'Argent, Seychelles</figcaption>
    </figure>
</script>

<script id="templateItem5" type="text/x-template">
    <figure class="img-container">
        <img src="css/carousel/images/rocks.jpeg" alt="rocks" style="height:100%; width: 100%" />
        <figcaption class="img-caption">Stonehenge, England</figcaption>
    </figure>
</script>

Press Ctrl+F5 (Windows) or +F5 (macOS) to run the app. Then, the Syncfusion ASP.NET Core Carousel control will be rendered in the default web browser.

ASP.NET Core Carousel Control

View Sample in GitHub.

See also