我有一个控制器:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebImageProcessor.Models;
using WebImageProcessor.ViewModel;
using WebImageProcessor.ViewModel.Models;
namespace WebImageProcessor.Areas.Admin.Controllers
{
[Area("Admin")]
public class HomeController : Controller
{
private readonly ImageProcessorDbContext db;
public HomeController(ImageProcessorDbContext context)
{
db = context;
}
[Authorize(Roles = "admin")]
public IActionResult Index()
{
AdminPageViewModel usersInf = new AdminPageViewModel();
usersInf.FullUsersInf = db.AppUsers
.Select(user => new FullUserInfModel
{
Nickname = user.Nickname,
Name = user.Name,
Surname = user.Surname,
Password = user.Password,
RoleName = user.Role.RoleName
})
.ToList();
return View(usersInf);
}
[Authorize(Roles = "admin")]
[HttpPost]
public async Task<IActionResult> ChangeUserRoleAsync(string? nickname)
{
if(!string.IsNullOrWhiteSpace(nickname))
{
AppUser user = db.AppUsers.FirstOrDefault(x => x.Nickname == nickname);
user.Role.RoleName = ChackAdminRole(nickname) ? "user" : "admin";
db.AppUsers.Update(user);
await db.SaveChangesAsync();
}
return RedirectToAction("Index");
}
private bool ChackAdminRole(string roleName)
{
if (roleName == "admin")
{
return true;
}
else
{
return false;
}
}
}
}
有局部视图:
@model WebImageProcessor.ViewModel.AdminPageViewModel
<!-- Table head options start -->
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Редагування користувачів</h4>
</div>
<div class="card-content collapse show">
<div class="table-responsive">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Nickname</th>
<th scope="col">Ім'я</th>
<th scope="col">Призвіще</th>
<th scope="col">Пароль</th>
<th scope="col">Роль</th>
<th scope="col">Змінити роль</th>
<th scope="col">Видалити</th>
</tr>
</thead>
<tbody>
@foreach(var user in Model.FullUsersInf)
{
<tr>
<th>@user.Nickname</th>
<th>@user.Name</th>
<th>@user.Surname</th>
<th>@user.Password</th>
<th>@user.RoleName</th>
<th>
<form asp-area="Admin" asp-controller="Home" asp-action="ChangeUserRoleAsync" method="post" asp-route-nickname="@user.Nickname">
@if(user.RoleName == "user")
{
<input class="btn btn-info adminTableBtn" type="submit" value="на admin" />
}
else
{
<input class="btn btn-info adminTableBtn" type="submit" value="на user" />
}
</form>
</th>
<th>
<form asp-controller="Home" asp-action="DeleteUser" method="post" asp-route-id="@user.Nickname">
<input class="btn btn-info adminTableBtn" type="submit" value="видалити" />
</form>
</th>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Table head options end -->
还有 Program.cs:
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using WebImageProcessor.Models;
using WebImageProcessor.Services.Image_process.Interfeces;
using WebImageProcessor.Services.Image_process.Realization;
using WebImageProcessor.Services.Image_Process.Interfeces;
namespace WebImageProcessor
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
//Add file whith configuration, which contains paths
builder.Configuration.AddJsonFile("pathsSettings.json");
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ImageProcessorDbContext>();
builder.Services.AddScoped<IDetectObject, YoloV8ProcessImgService>();
builder.Services.AddScoped<IPhotoInformation, PhotoInfYoloV8Service>();
/////////////////////////////////////////////////////
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/LoginLogoutReg/Login";
options.AccessDeniedPath = "/Home/Index";
});
builder.Services.AddAuthorization();
/////////////////////////////////////////////////////
var app = builder.Build();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthorization();
app.MapControllerRoute(
name: "admin",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
}
}
}
我的问题是,当我单击角色更改按钮时,显示路径未知的错误:未找到主机页面 localhost 未找到网址 https://localhost:7134/Admin/Home/ChangeUserRoleAsync? 的网页?昵称=DewoLanosOneLove HTTP 错误 404
我一直在尝试查找有关此问题的信息,但我尝试过的所有方法均无效。
我不知道怎么做,但我解决了导航问题,以一种我不清楚的方式,我重写了方法,但删除了异步部分,一切都开始工作。我不知道它是如何工作的,因为我确信控制器中显示的名称是相同的并且是用英文写的。