You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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")
  • If neither is present, add the code snippet above to your navbar, or create the _LoginPartial.cshtml partial view in Views/Shared with 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.EntityFramework
    • Microsoft.AspNet.Identity.Core
    • Microsoft.Owin.Security.Cookies
  • In your Startup.cs file, add authentication middleware in the Configuration method:
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login")
    });
    
  • Make sure you have an AccountController that 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 an ApplicationUser model).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:52:28