"This page could not be found on localhost" - 添加 Area 后,在 ASP.NET MVC Core 2.2 中

"This page could not be found on localhost" - after adding Area, in ASP.NET MVC Core 2.2

我的项目被重建到名为 "area" 的文件夹中,之前一切正常,用户被授权访问帐户管理面板。添加这些区域后,项目看不到它应该去哪里。

我的结构项目:

区域"Authorize"中的结构:

我尝试使用 Startup.cs 中区域的路线:

...
            services.AddMvc( options =>
            options.EnableEndpointRouting = false)
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
...
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
                routes.MapAreaRoute(
                    name: "MyAreaAuthorize",
                    areaName: "Authorize",
                    template: "Authorize/{controller=Account}/{action=Index}/{id?}");
                routes.MapAreaRoute(
                    name: "MyAreaManage",
                    areaName: "Authorize",
                    template: "Authorize/{controller=Manage}/{action=Index}/{id?}");
                routes.MapAreaRoute(
                    name: "MyAreaAdminAccount",
                    areaName: "AdminAccount",
                    template: "AdminAccount/{controller=Admin}/{action=Index}/{id?}");
            });

我还复制了 _ViewImports.cshtml 到包含每个区域视图的文件夹。

我的文件如下所示:

@using Example
@using Example.Models
@using Example.AccountArea.Models
@using Microsoft.AspNetCore.Identity
@addTagHelper Example.TagHelpers.*, Example
@addTagHelper Identity.TagHelpers.*, Identity
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

查看区域"Authorize":

@using Example.AccountArea.Models;
@using Microsoft.AspNetCore.Identity;
@model Example.AccountArea.Models.AccountModel.LoginModel;
@inject SignInManager<UserApp> Sign
@{
    ViewData["Title"] = "Zaloguj się";
}

<form asp-area="Authorize" asp-controller="Account" asp-action="Login" method="post">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div>
                    <input asp-for="Email" placeholder="Email" />
                    <span asp-validation-for="Email" class="text-danger margin-text-form"></span>
                </div>
                <div>
                    <input asp-for="Password" placeholder="Hasło" />
                    <span asp-validation-for="Password" class="text-danger margin-text-form"></span>
                </div>
                <div class="group">
                    <div class="checkbox">
                        <input type='checkbox' id='my-checkbox'>
                        <label for='my-checkbox'>@Html.DisplayNameFor(m => m.RememberMe)</label>
                    </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn-dark">Login</button>
                </div>
                <div class="group">
                    <p>
                        <a asp-area="Authorize" asp-controller="Account" asp-action="ForgotPassword">Forgot?</a>
                    </p>
                    <p>
                        <a asp-controller="Account" asp-action="Register">Register</a>
                    </p>
                </div>
            </form>
        </section>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

区域“授权”中的控制器帐户:

控制器已包含属于 "Authorize" 区域的 class 的信息

using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authentication;
using System;
using Example.AccountArea.Models;
using Example.AccountArea.Models.AccountModel;

namespace Example.AccountArea.Controllers
{
    [Area("Authorize")]
    public class AccountController : Controller
    {
        ...My code is irrelevant to the problem...

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Login()
        {
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(LoginModel login)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(login.Email, login.Password, login.RememberMe, false);
                if (result.Succeeded)
                {
                    _logger.LogInformation("...");
                    return RedirectToAction(nameof(ManageController.Index), "Manage");
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("...");
                    return RedirectToAction(nameof(LockoutOptions));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "...");
                    return View("../Home/Index");
                }
            }

            // If we got this far, something failed, redisplay form
            return View("../Home/Index", login);
        }

        ...My code is irrelevant to the problem...
    }
}

启动应用程序后,向我显示登录表单,链接不起作用,消息类似于下面的消息,未通过数据验证,单击 "Log in" 后显示:

浏览器页面的物理转换:

来自登录:

收件人:

请不要告诉我,它是复制的,并且有类似的解决方案,因为我先在其他类似的帖子中找到了解决方案!

有什么想法吗? 如果你能帮助我,谢谢你..

您的 Authorize 使用不同的默认控制器确实有两条区域路线...我相信您应该只有一条。不确定这是否是问题的原因。但我认为这可能会发生冲突,它看起来也没有必要,因为默认情况下,该区域中的任何控制器都将包含在单个路由中。

我的aspnet core 2.2 app专区注册是这样的:

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "Admin",
                template: "{area:exists}/{controller=Admin}/{action=Index}/{id?}");
...