此上下文Site.master不支持ASP.NET代码块

时间:2016-04-20 00:43:10

标签: c# asp.net

我试图在我的母版页中编写一些代码来检查用户是否已登录。这是代码块

            <div class="navbar-collapse collapse">
                <asp:LoginView runat="server" ViewStateMode="Disabled">
                    <% 

                    if (Session["UserID"] == null) {
                    %>
                    <AnonymousTemplate>
                        <ul class="nav navbar-nav navbar-right">
                            <li><a runat="server" href="~/Account/Register">Register</a></li>
                            <li><a runat="server" href="~/Account/Login">Log in</a></li>
                        </ul>
                    </AnonymousTemplate>
                    <%
                    } else  { 
                    %>
                    <LoggedInTemplate>
                        <ul class="nav navbar-nav navbar-right">
                            <li><a runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Session["UserID"] %>!</a></li>
                            <li>
                                <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
                            </li>
                        </ul>
                    </LoggedInTemplate>
                    <%
                    }
                    %>
                </asp:LoginView>
            </div>

1 个答案:

答案 0 :(得分:4)

代码块,如

<% if (Session["UserID"] == null) {
                %> ...

不允许进入<asp: runat="server">