Visual Studio 2017中ASP.NET MVC项目导航栏无登录注册入口问题咨询
Hey there, let's walk through the most common reasons why your login and register links aren't showing up in the navbar of your new ASP.NET MVC project in Visual Studio 2017, and how to fix them:
1. You chose the wrong authentication template when creating the project
When you create an ASP.NET Web Application (MVC), the default might be set to No Authentication—which skips all identity-related components like login/register UI and logic. Here's how to address this:
- If you're okay with starting fresh, create a new project again: when you reach the "New ASP.NET Web Application" window, select MVC, click the Change Authentication button, and choose Individual User Accounts. This will auto-generate all the login/register pages and navbar links out of the box.
- If you want to keep your existing project, you'll need to manually add identity support (jump to point 3 for details).
2. The login partial view is missing from your layout page
The login/register links are usually rendered via a partial view in your main layout. Check your Views/Shared/_Layout.cshtml file:
- Look for a section in the navbar (usually inside a
<ul class="nav navbar-nav navbar-right">element) that includes either:- Direct code to render the links like this:
<ul class="nav navbar-nav navbar-right"> @if (User.Identity.IsAuthenticated) { <li>@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", null, new { title = "Manage" })</li> <li><a href="@Url.Action("Logout", "Account")">Log off</a></li> } else { <li>@Html.ActionLink("Register", "Register", "Account", null, new { id = "registerLink" })</li> <li>@Html.ActionLink("Log in", "Login", "Account", null, new { id = "loginLink" })</li> } </ul> - Or a partial view reference:
@await Html.PartialAsync("_LoginPartial")
- Direct code to render the links like this:
- If neither is present, add the code snippet above to your navbar, or create the
_LoginPartial.cshtmlpartial view inViews/Sharedwith the same logic.
3. Identity packages and configuration are missing
If your project didn't include identity from the start, you'll need to add the necessary packages and configure authentication:
- Open the NuGet Package Manager and install these packages:
Microsoft.AspNet.Identity.EntityFrameworkMicrosoft.AspNet.Identity.CoreMicrosoft.Owin.Security.Cookies
- In your
Startup.csfile, add authentication middleware in theConfigurationmethod:app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login") }); - Make sure you have an
AccountControllerthat handles login/register actions, along with corresponding views for those actions. You can scaffold these using Visual Studio's "Add Controller" wizard (select MVC 5 Controller with views, using Entity Framework, and tie it to anApplicationUsermodel).
4. You're already logged in (unlikely for a new project, but worth checking)
If you accidentally logged in during testing, the navbar will show your username and a "Log off" link instead of login/register. Try opening your site in an incognito/private browser window, or clear your browser's cookies to see if the links reappear.
内容的提问来源于stack exchange,提问作者Seyed Amir Mousavian

