2009-06-24 42 views

回答

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

您可能想要先檢查User.Identity是否爲null,然後再試圖獲取名稱。

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; 
    }