I keep getting this error

Parser Error Message: Encountered end tag "nav" with no matching start tag.  Are your start/end tags properly balanced?


Source Error:

Line 110:                         </li>
Line 111:                         </ul>
Line 112:                 </nav>
Line 113:            }    
Line 114:       </div>

My code is fairly simple (using Webmatrix with Razor)

@if (WebSecurity.IsAuthenticated) {     
      <nav>
        <ul>
            
            <li id="login">
                <a id="login-trigger" href="#">
                                        <span id="user-panel-check"></span>
               
                                        <span id="user-panel-title">Welcome, <strong>@WebSecurity.CurrentUserName</strong></span>
                
                    <span id="user-panel"></span>
                </a>
                <div id="login-content">
                  <ul>
                  <li><a href="#"><span>Settings</span> <img src="./images/setting.png" alt=""></a></li>
                  <li><a href="#"><span>Help</span> <img src="./images/help.png" alt=""></a></li>
                  <li><a href="./index.html"><span>Log Out</span> <img src="./images/logout.png" alt=""></a></li>
                  </ul>
               </div>                       
            </li> 
            
        </ul>
      </nav> 
          } else {
                 <nav>
                     <ul>
            
                     <li id="login">
                <a id="login-trigger" href="#">
                                        <span id="user-panel-check"></span>
               
                                        <span id="user-panel-title">Login</strong></span>
                
                    <span id="user-panel"></span>
                </a>   
                         </li>
                         </ul>
                 </nav>
            }

Please advise.

Dani AI

Generated

As noted, this is a classic unbalanced-HTML problem. In the snippet you posted the else branch contains a stray closing </strong> with no matching opening tag, and Razor's parser tries to balance HTML across the server-side block. That single mismatched tag can confuse the parser and make it think the closing </nav> has no start tag.

Quick fixes (pick one): replace the broken fragment with a plain span, or wrap the text in a matched strong tag.

<span id="user-panel-title">Login</span>

or

<span id="user-panel-title"><strong>Login</strong></span>

After fixing that line, refresh the page — the parser error should go away. While you are at it, scan the whole if/else block for other mismatched or mis-nested tags (ul/li pairs, stray closing tags, etc.), or run the rendered HTML through a validator.

If you want to avoid Razor balancing quirks altogether, either move the repeated nav outside the conditional (as suggested to reduce duplication) or make the HTML literal inside the code block using a text block. Example:

@if (WebSecurity.IsAuthenticated) {
  <text>
    <nav>
      ...
    </nav>
  </text>
} else {
  ...
}

Checklist: fix the stray </strong>, validate HTML, and consider factoring the nav out for clarity.

Recommended Answers

All 2 Replies

Member Avatar for Member #905211

Why don't you put this outside of the if statement:

<nav>
<ul>
...
</ul>
</nav>

This doesn't say why it's happening, however it will get rid of some redundancy.

On line 32 you have a closing </strong> tag without an opening one. That might have something to do with it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.