我有一个应用程序,我想在部分视图中显示所有登录用户的列表。我知道我们可以使用成员资格来完成它,但不确定如何完全实现它。
我的控制器代码
[Authorize(Roles = "Admin")]
public ActionResult ActiveUsers()
{
//ViewBag.Message = "Your contact page.";
// MembershipUserCollection users;
// Bind users to ListBox.
List<MembershipUser> onlineUsers = new List<MembershipUser>();
foreach (MembershipUser user in Membership.GetAllUsers())
{
if (user.IsOnline)
{
onlineUsers.Add(user);
HttpRuntime.Cache["onlineUsers"] = onlineUsers;
}
}
return View();
}
查看
@if (HttpRuntime.Cache["onlineUsers"] != null)
{
List<string> LoggedOnUsers = (List<string>)HttpRuntime.Cache["onlineUsers"];
if (LoggedOnUsers.Count > 0)
{
<div class="ChatBox">
<ul>
@foreach (string user in LoggedOnUsers)
{
<li>
<div class="r_row">
<div class="r_name">@Html.Encode(user)</div>
</div>
</li>
}
</ul>
</div>
}
}
当我访问活动用户页面时,使用上面的代码,它引发了我的异常
Server Error in '/' Application.
Specified method is not supported.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: Specified method is not supported.
Source Error:
Line 58: // Bind users to ListBox.
Line 59: List<MembershipUser> onlineUsers = new List<MembershipUser>();
Line 60: foreach (MembershipUser user in Membership.GetAllUsers())
Line 61: {
Line 62:
我能有更好的方法吗?