2009-06-24 5 views

答えて

17
public static string YourHtmlHelper(this HtmlHelper html) 
{ 
    var name = html.ViewContext.HttpContext.User.Identity.Name; 
} 
5

名前を取得しようとする前に、User.Identityが最初にヌルであるかどうかを確認したい場合があります。

public static string YourHtmlHelper(this HtmlHelper html) 
    { 
     var identity = html.ViewContext.HttpContext.User.Identity; 

     if (identity != null) 
     { 
      return html.ViewContext.HttpContext.User.Identity.Name; 
     } 

     return string.Empty; 
    } 
関連する問題