How to Enable Carousels in Blazor WebAssembly

This example is using ASP.NET Core 3.1 but it should work with .NET 5 and later.

It is incredibly easy to enable the Bootstrap v4.3.1 carousel component. It requires that some JavaScript files be added to the project's index.html file.

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

Then the function startCarousels() must be created.

function startCarousels() {
	$('.carousel').carousel({ interval: 4000 });
}

index.html

This will result in an index.html file that looks like this (this file may vary slightly depending on the version of Blazor):

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	<title>LoginForm</title>
	<base href="/" />
	<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
	<link href="css/app.css" rel="stylesheet" />
	<script type="text/javascript">
		function startCarousels() { // NOTE: this function added
			$('.carousel').carousel({ interval: 4000 });
		}
	</script>
</head>

<body>
	<app>Loading...</app>

	<div id="blazor-error-ui">
		An unhandled error has occurred.
		<a href="" class="reload">Reload</a>
		<a class="dismiss">🗙</a>
	</div>

	<script src="_framework/blazor.webassembly.js"></script>

    <!-- NOTE: THE FOLLOWING THREE LINES ADDED -->
	<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

</body>

</html>

Create a RAZOR Page for the Carousel

Create or modify a razor page that injects the IJSRuntime interface and use it to call the startCarousels JavaScript function.

@page "/carousel"
@inject IJSRuntime JSRuntime

<h3>Carousel</h3>
<div class="bd-example">
    <div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
            <li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li>
            <li data-target="#carouselExampleCaptions" data-slide-to="1"></li>
            <li data-target="#carouselExampleCaptions" data-slide-to="2"></li>
        </ol>
        <div class="carousel-inner">
            <div class="carousel-item active">
                <img src="https://picsum.photos/id/196/1920/1080" class="d-block w-100">
                <div class="carousel-caption d-none d-md-block">
                    <h5>First slide label</h5>
                    <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
                </div>
            </div>
            <div class="carousel-item">
                <img src="https://picsum.photos/id/1057/1920/1080" class="d-block w-100">
                <div class="carousel-caption d-none d-md-block">
                    <h5>Second slide label</h5>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
                </div>
            </div>
            <div class="carousel-item">
                <img src="https://picsum.photos/id/1067/1920/1080" class="d-block w-100">
                <div class="carousel-caption d-none d-md-block">
                    <h5>Third slide label</h5>
                    <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
                </div>
            </div>
        </div>
        <a class="carousel-control-prev" href="#carouselExampleCaptions" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#carouselExampleCaptions" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
</div>

@code {

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await JSRuntime.InvokeVoidAsync("startCarousels");
    }
}

That's all that's needed to enable Bootstrap carousels.

Coding Video

https://youtu.be/IfkmFGYBSOA


Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site.
OK