简体   繁体   English

无法有条件地获取html渲染?

[英]Can't get the html render conditionally?

I have a strange incident in getting the html page rendering conditionally with asp.net core 2.0 我在使用ASP.NET Core 2.0有条件地获取html页面时遇到了一个奇怪的事件

    <head>
        @{
                var user = await _userManager.FindByNameAsync(User.Identity.Name);
         }
    </head>
    <body>
     @if (user != null)
     {
       <a href="/user/edit/?username=@user.UserName" class="dropdown-toggle">
       <span class="hidden-xs">@user.FullName</span>
                                   </a>
     }
   </body>

For this layout, it gives errors: "The name 'user' does not exist in the current context It used to work fine in .net core 1.1 and classic asp.net. 对于此布局,它会出现错误: “名称'user'在当前上下文中不存在。它过去在.net core 1.1和传统的asp.net中正常工作。

However, if I do like this, then it works fine. 但是,如果我喜欢这样,那就可以了。 So strange. 这么奇怪。

<body>
@{
    var user = await _userManager.FindByNameAsync(User.Identity.Name);
   if (user != null)
    {
      <a href="/user/edit/?username=@user.UserName" class="dropdown-toggle">
       <span class="hidden-xs">@user.FullName</span>
                               </a>
     }
 }

It seems you can't declare variables inside "head" tag. 看来您无法在“ head”标签内声明变量。 I tried the following and it worked just fine: 我尝试了以下方法,但效果很好:

@{
    var user = await _userManager.FindByNameAsync(User.Identity.Name);
}
<head>
    ...
</head>
<body>
@if (user != null)
{
    <a href="/user/edit/?username=@user.UserName" class="dropdown-toggle">
        <span class="hidden-xs">@user.FullName</span>
    </a>
}
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM