我找不到User.Identity.Name 来自哪个引用程序集。我如何找到它?我需要将它添加到类库而不是我制作的默认 MVC 5 应用程序,这样我才能访问它吗?
http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx
C# 类库/AUser.cs
public class AUser
{
public string Username
{
get
{
return User.Identity.Name // doesn't notice User
}
}
}
最佳答案
似乎在 System.Web 命名空间中。
http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx
编辑
根据您的附加代码,您必须包含完全限定名称 - 例如:
HttpContext.Current.User.Identity.Name
当您在 Controller 之外(或在另一个类中)时。请注意,如果在没有 HttpContext 的任何情况下使用这个 MVC 类之外的类,它将失败(出现 NullReferenceException)。为确保我们没有任何这些,我们通常将 HttpContext 或我们需要的部分(用户名、Url 等)作为函数参数传递。
关于c# - User.Identity.Name - 哪个程序集引用将其引入您的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20672127/