为什么bootstrap在我的ASP.NET Core中不起作用?

f45qwnt8  于 2023-10-21  发布在  .NET
关注(0)|答案(2)|浏览(165)

我在做简单的Web应用程序在ASP.NET中,我想显示从菜单选项,但它不显示任何选项。在我在学校做的另一个应用程序中,它运行得很好。下面是我的代码,它不工作:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Blogic Project</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
     @await RenderSectionAsync("Styles", required: false)
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark border-bottom box-shadow mb-3">
            <div class="container-fluid">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Blogic Project</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-light" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-light" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>

                     @{
                        @if (User.Identity.IsAuthenticated == false)
                        {
                            <ul class="navbar-nav navbar-right">
                                <li class="nav-item"><a class="nav-link text-light" asp-area="Security" asp-controller="Account" asp-action="Register"><span class="glyphicon"></span> Sign Up</a></li>
                                <li class="nav-item"><a class="nav-link text-light" asp-area="Security" asp-controller="Account" asp-action="Login"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
                            </ul>
                        }
                        else
                        {
                            <ul class="nav navbar-nav navbar-right">
                                <li class="nav-item dropdown">
                                    <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">Logged as @User.FindFirstValue(ClaimTypes.Name)</a>
                                    <ul class="dropdown-menu dropdown-menu-dark">
                                        <li class="nav-item"><a class="nav-link text-light" asp-area="Security" asp-controller="Account" asp-action="Logout"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>
                                    </ul>
                                </li>
                            </ul>
                        }
                    }
                </div>
            </div>
        </nav>
    </header>
    <div class="parallax">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top noparallax-footer footer text-muted bg-dark border-bottom box-shadow">
        <div class="container">
            &copy; 2022 - BlogicProject - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

问题就出在这个标签上:

<ul class="nav navbar-nav navbar-right">
   <li class="nav-item dropdown">
                                    <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">Logged as @User.FindFirstValue(ClaimTypes.Name)</a>
       <ul class="dropdown-menu dropdown-menu-dark">
           <li class="nav-item"><a class="nav-link text-light" asp-area="Security" asp-controller="Account" asp-action="Logout"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>
       </ul>
   </li>
</ul>

希望从另一个项目输出:

我还是一个学生,这是我第二次在ASP.NET中做一些事情。
谢谢
编辑:尝试在引导之前添加jquery,仍然不起作用。
编辑2:与这种变化一起工作

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"> </script>
8fsztsew

8fsztsew1#

你必须在Bootstrap之前使用jQuery。
你可以看到两个结果:

ldioqlga

ldioqlga2#

在ASP.NET Core中,使用Libman安装JQuery和Bootstrap 5后,在_Layout.cshtml中添加以下内容:

<script src="~/lib/bootstrap/js/bootstrap.bundle.js"></script>

供参考https://getbootstrap.com/docs/5.0/components/dropdowns/

相关问题