尝试从 Azure 应用程序服务访问视图时出现 ERR_TOO_MANY_REDIRECTS,但从 Visual Studio 运行时工作正常。
在 Visual Studio 2022 中使用 Asp.net Core 6 和 jQuery JavaScript 库 v3.6.0
使用 - 访问该页面
<li class="nav-item">
<a class="dropdown-item" asp-area="" asp-controller="UserList" asp-action="UserList">User List</a>
</li>
控制器 -
public async Task<IActionResult> UserList()
{
List<UserListViewModel> users = await _userListControllerHelper.MapToViewModel(_context);
return View(users);
}
MapToViewModel -
public async Task<List<UserListViewModel>> MapToViewModel(ApplicationDbContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
List<UserListViewModel> model = new List<UserListViewModel>();
foreach (var user in context.Users)
{
var userDetails = new UserListViewModel();
userDetails.Username = user.UserName ?? string.Empty;
var roles = await _userManager.GetRolesAsync(user);
foreach (var role in roles)
{
userDetails.Role += role;
}
userDetails.Email = user.Email ?? string.Empty;
IEnumerable<UsersHospital> usersHospitals = _usersHosptialRepository.GetHospitalIdsFromUserId(user.Id);
userDetails.UsersHospitalId = usersHospitals is not null ? usersHospitals.FirstOrDefault().id : 0;
userDetails.Hospital = usersHospitals is not null ? _lookupRepository.EnabledHospitals().FirstOrDefault(x => x.HospitalId == usersHospitals.FirstOrDefault().HospitalId).Hospital : string.Empty;
userDetails.HasEmailBeenConfirmed = user.EmailConfirmed;
userDetails.IsLockedOut = user.LockoutEnabled && (user.LockoutEnd is not null && user.LockoutEnd > DateTime.Now);
model.Add(userDetails);
}
return model.ToList();
}
查看-
@model IEnumerable<UserListViewModel>
@section breadcrumb{
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item active" aria-current="page">User List</li>
</ol>
</nav>
}
@if (TempData["AlertMessage"] != null)
{
<div class="alert alert-message alert-success">
@TempData["alertMessage"]
</div>
}
<div class="row">
<div class="card col-md-12 border-0">
<div class="card-body align-content-center">
<a class="btn btn-primary btn-block" asp-area="Identity" asp-page="/Account/Register">Register New User</a>
</div>
</div>
</div>
<div class="row mb-5">
<div class="card-deck col-md-12 overflow-auto">
<div class="card col-md-12 border p-0">
<div class="card-header">
<b>List of Active Users</b>
</div>
<div class="UsersList card-body align-content-center">
<div class="row p-1 border-1 border-bottom border-top flex-container">
<div class="col-xxl-2 col-xl-2 col-lg-2 col-md-2 border-1 border-end"><b>Username</b></div>
<div class="col-xxl-2 col-xl-2 col-lg-2 col-md-2 border-1 border-end"><b>User Email</b></div>
<div class="col-xxl-1 col-xl-1 col-lg-1 col-md-1 border-1 border-end"><b>User Email Confirmed</b></div>
<div class="col-xxl-2 col-xl-2 col-lg-2 col-md-2 border-1 border-end"><b>User Role</b></div>
<div class="col-xxl-2 col-xl-2 col-lg-2 col-md-2 border-1 border-end"><b>User Location</b></div>
<div class="col-xxl-3 col-xl-3 col-lg-3 col-md-3"><b>Actions</b></div>
</div>
@if (Model != null)
{
@foreach (var item in Model)
{
@if (!item.IsLockedOut)
{
<div class="row p-1 border-1 border-bottom flex-container card-deck">
<div class="card col-xxl-2 col-xl-2 col-lg-2 col-md-2 col-sm-2 border-1 border-end">@item.Username</div>
<div class="card col-xxl-2 col-xl-2 col-lg-2 col-md-2 col-sm-2 border-1 border-end">@item.Email</div>
<div class="card col-xxl-1 col-xl-1 col-lg-1 col-md-1 border-1 border-end">
@item.HasEmailBeenConfirmed
@if (!item.HasEmailBeenConfirmed)
{
<br />
<a asp-controller="UserList" asp-action="ResendConfirmationEmail" class="btn btn-primary btn-block" asp-route-useremail="@item.Email" asp-route-returnUrl="@Url.Action("UserList", "UserList", new { area = "" })">
Resend
</a>
}
</div>
<div class="card col-xxl-2 col-xl-2 col-lg-2 col-md-2 border-1 border-end">@Html.DisplayFor(modelItem => item.Role)</div>
<div class="card col-xxl-2 col-xl-2 col-lg-2 col-md-2 border-1 border-end">@Html.DisplayFor(modelItem => item.Hospital)</div>
<div class ="col-xxl-3 col-xl-3 col-lg-3 col-md-3">
<a asp-controller="UserList" method="get" asp-action="UserEdit" asp-route-userEmail="@item.Email" class="btn btn-primary btn-block">
Edit User
</a>
<button class="lockUnlockOrDeleteUser btn btn-primary btn-block" data-id="@item.Email" data-type="lock">
Lock User
</button>
<button class="lockUnlockOrDeleteUser btn btn-danger btn-block" data-id="@item.Email" data-type="delete">
Delete User
</button>
</div>
</div>
}
}
}
</div>
</div>
</div>
</div>
<div class="row">
<div class="card-deck col-md-12 overflow-auto">
<div class="card col-md-12 border p-0">
<div class="card-header">
<b>List of Locked Out Users</b>
</div>
<div class="UsersList card-body align-content-center">
<div class="row p-1 border-1 border-bottom border-top flex-container">
<div class="col-xxl col-xl col-lg col-md border-1 border-end"><b>Username</b></div>
<div class="col-xxl col-xl col-lg col-md border-1 border-end"><b>User Role</b></div>
<div class="col-xxl col-xl col-lg col-md border-1 border-end"><b>User Email</b></div>
<div class="col-xxl col-xl col-lg col-md border-1 border-end"><b>User Location</b></div>
<div class="col-xxl-3 col-xl-3 col-lg-3 col-md-3"><b>Actions</b></div>
</div>
@if (Model != null)
{
@foreach (var item in Model)
{
@if (item.IsLockedOut)
{
<div class="row p-1 border-1 border-bottom flex-container">
<div class="col-xxl col-xl col-lg col-md border-1 border-end">@item.Username</div>
<div class="col-xxl col-xl col-lg col-md border-1 border-end">@item.Role</div>
<div class="col-xxl col-xl col-lg col-md border-1 border-end">@item.Email</div>
<div class="col-xxl col-xl col-lg col-md border-1 border-end">@item.Hospital</div>
<div class="col-xxl-3 col-xl-3 col-lg-3 col-md-3">
<button class="lockUnlockOrDeleteUser btn btn-primary btn-block" data-id="@item.Email" data-type="unlock">
Unlock User
</button>
<button class="lockUnlockOrDeleteUser btn btn-danger btn-block" data-id="@item.Email" data-type="delete">
Delete User
</button>
</div>
</div>
}
}
}
</div>
</div>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
$(document).on('click', '.lockUnlockOrDeleteUser', (event) => {
event.preventDefault();
if (confirm("Are you sure you want to " + event.target.dataset.type + " this user?: " + event.target.dataset.id)) {
$.ajax({
url: '@Url.Action("LockUnlockOrDeleteUser", "UserList")',
type: 'POST',
data: { userEmail: event.target.dataset.id, type: event.target.dataset.type },
})
.done(function (response) {
switch (response.succeeded) {
case false:
alert("Failed to " + event.target.dataset.type + " user, please contact FNSD administration for help.");
break;
case true:
alert("User successfully " + event.target.dataset.type + "ed");
var urlToRedirect = '@Url.Action("userlist", "userlist")';
window.location.href = urlToRedirect;
break;
default:
alert(JSON.stringify(response) + ": Please contact FNSD administration for help.");
}
})
.fail(function (jqXHR, textStatus, errorThrown) {
alert("AJAX request failed: " + errorThrown + ": Please contact FNSD administration for help.");
});
};
});
</script>
}
我不知道为什么会发生这种情况。 尝试按照它的说明删除所有cookie,但这并不能解决问题。
如有任何帮助,我们将不胜感激。
发现了一些事情。 我收到 ERR_TOO_MANY_REDIRECTS 的原因是由于系统正在寻找 PageNotFound 视图,但没有。 所以添加了一个并解决了这个问题。
加载用户列表页面时给出状态代码:302 Found。 这是一个中间件问题 - 类别: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware 事件ID:1 跨度 ID:2ff3e9926a1b6c03 跟踪 ID:7314731274212a962b5c1323b0299200 父 ID: 0000000000000000 请求 ID:400070d4-0005-f900-b63f-84710c7967bb 请求路径:/UserAdmin/UserList 执行请求时发生未处理的异常。
问题是 System.InvalidOperationException:已经有一个与此连接关联的打开的 DataReader,必须先将其关闭。
通过添加解决了这个问题
MultipleActiveResultSets=True
连接字符串。
谢谢您的帮助。