A field initializer cannot reference the non-static field, method, or property 'Controller.HttpContext'

前端 未结 1 879
无人及你
无人及你 2021-01-27 10:59

I am trying to get the user that is using the application in the controller by doing this:

public class TableNameController : Controller
{
    private ConnectionS         


        
相关标签:
1条回答
  • 2021-01-27 11:28

    You cannot use field initializer for what you trying to do, but you can easily use instance constructor for that:

    public class TableNameController : Controller
    {
        ...
        private string userIdentity;
    
        public TableNameController() {
            userIdentity = HttpContext.User.Identity.Name.Split('\\')[1].Replace(".", " ");
        }
    
        ...
    
    0 讨论(0)
提交回复
热议问题