Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

asp net bootstrap 5 navigation bar

//If your bootstrap navbar breaks in ASP.NET when updating to bootstrap 5.

//So I first exported my navbar into a partial view and modified the code as follows
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
        @Html.ActionLink( "Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" } )
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                <li class="nav-item">
                    @Html.ActionLink( "Home", "Index", "Home", null, new { @class = "nav-link" } )
                </li>
                
                <li class="nav-item">
                    @Html.ActionLink( "About", "About", "Home", null, new { @class = "nav-link" } )
                </li>
                
                <li class="nav-item">
                    @Html.ActionLink( "Contact", "Contact", "Home", null, new { @class = "nav-link" } )
                </li>
            </ul>
           
            //these are the register and login links, I have individual user atuh enabled
            @Html.Partial( "_LoginPartial" )
        </div>
    </div>
</nav>


//If you have the register and login buttons on the navbar,
//Then navigate to ~/views/shared/_LoginPartial.cshtml and modify the code:
@using Microsoft.AspNet.Identity
@if ( Request.IsAuthenticated )
{
    using ( Html.BeginForm( "LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right nav-link" } ) )
    {
        @Html.AntiForgeryToken()

        <ul class="nav navbar-nav navbar-right">
            <li>
                @Html.ActionLink( "Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" } )
            </li>
            <li><a href="javascript:document.getElementById('logoutForm').submit()" class="nav-link">Log off</a></li>
        </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li class="nav-item">
            @Html.ActionLink( "Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink", @class = "nav-link" } )
        </li>
        <li class="nav-item">
            @Html.ActionLink( "Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink", @class = "nav-link" } )
        </li>
    </ul>
}


//Now, call the navbar partial view in the _Layout.cshtml. Otherwise you can just replace
//the navbar in layout.cshtml with the first batch of code above.

//-------------------------------------------------------------------------------

//Sometimes after the BS5 update, when running your app you'll get an error on the bootstrap script section in _Layout.cshtml.
//To fix this, in ~/App_Start/BundleConfig.cs, locate this code.

bundles.Add( new ScriptBundle( "~/bundles/bootstrap" ).Include(
                      "~/Scripts/bootstrap.js" ) );

//Modify it so it looks like this (change new ScriptBundle to new Bundle):

bundles.Add( new Bundle( "~/bundles/bootstrap" ).Include(
                      "~/Scripts/bootstrap.js" ) );


//Hope this helps!
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# unity follow object 
Csharp :: transformar de string a int c# 
Csharp :: json to httpcontent c# 
Csharp :: clear textbox winform 
Csharp :: how to change textMesh Pro unity 
Csharp :: abril modal boostrap 
Csharp :: how to check if textbox is empty in c# 
Csharp :: unity instantiate with name 
Csharp :: programmatically write bash script from c# 
Csharp :: wpf restart application c# 
Csharp :: c# pick a random item from array 
Csharp :: c# error CS0515 
Csharp :: milliseconds to seconds c# 
Csharp :: call function from another script unity 
Csharp :: c# string default value 
Csharp :: hello world c# 
Csharp :: replace elements with greatest element on right side 
Csharp :: get hard disk serial number 
Csharp :: how to copy last element in list c# 
Csharp :: c# datagridview filter textbox 
Csharp :: c# void 
Csharp :: void ontriggerenter not working 
Csharp :: javascript close page after 5 seconds 
Csharp :: unity destroy after time 
Csharp :: c# using file.io 
Csharp :: c# list remove item based on property duplicate 
Csharp :: C# decimal with two places store as string with two places 
Csharp :: switch case c# contains 
Csharp :: get type of variable c# 
Csharp :: c# convert string to url encoding 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =