我对 asp.net 很陌生,最近我遇到了一个例外:
System.InvalidOperationException
例外的细节表明:
尚未为此应用程序或请求配置会话。
下面是发生这种情况的代码片段:
[HttpPost]
public object Post([FromBody]loginCredentials value)
{
if (value.username.Equals("Admin")
&&
value.password.Equals("admin"))
{
HttpContext.Session.Set("userLogin", System.Text.UTF8Encoding.UTF8.GetBytes(value.username)); //This the line that throws the exception.
return new
{
account = new
{
email = value.username
}
};
}
throw new UnauthorizedAccessException("invalid credentials");
}
我不知道为什么会这样或者这个错误到底是什么意思。 有人能解释一下是什么引起的吗?