C# 如何制作ActionLink';s文本响应用户是否经过身份验证?

C# 如何制作ActionLink';s文本响应用户是否经过身份验证?,c#,asp.net,model-view-controller,C#,Asp.net,Model View Controller,我希望ActionLink的文本属性在“User.Identity.Name”属性为空时显示为“Login” 当用户登录时,我希望ActionLink的text属性显示用户名 如何使我的ActionLink的文本属性响应这两种情况 _LayoutPage's code: @Html.ActionLink((string)User.Identity.Name.ToString(), "Login", "Home", new { @Class = "navbar-login", id = "na

我希望ActionLink的文本属性在“User.Identity.Name”属性为空时显示为“Login”

当用户登录时,我希望ActionLink的text属性显示用户名

如何使我的ActionLink的文本属性响应这两种情况

_LayoutPage's code:
  @Html.ActionLink((string)User.Identity.Name.ToString(), "Login", "Home", new { @Class = "navbar-login", id = "navbar-login" })

Controller's code:  
        public ActionResult _LayoutPage()
        {
            bool val1 = (System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
            if (val1 == true)
            {
                // If current user is logged in, display the current users name.
                return View();
            }
            else
            {
                // If user is null just say "Login"
                return View();
            }
        }

当用户名不为空时,在登录操作链接中显示用户名。当用户名为空时,显示“登录”


很好的代码,帮助很大。我需要将此代码与您的代码一起添加以使其正常工作。如果(User.Identity.Name==null | | User.Identity.Name==“”){loginDisplayText=“Login”}如果要检查字符串是否为null或空,还可以使用
string.IsNullOrEmpty()
@{ 
    string loginDisplayText = User.Identity.Name ?? "Login";
}
@Html.ActionLink(loginDisplayText, "Login", "Home", new { @class = "navbar-login", id = "navbar-login" })