RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-229433

User's questions

Martin Hope
User
Asked: 2022-09-08 15:26:55 +0000 UTC

用新的替换旧的控制器链接[关闭]

  • 0
关闭。这个问题不可能给出客观的答案。目前不接受回复。

想改进这个问题? 重新构建问题,以便可以根据事实和引用来回答。

2 个月前关闭。

改进问题

我正在制作我的宠物项目以熟悉 asp.net core mvc。最初,有一个控制器User负责与用户一起工作(注册、授权、密码更改和退出),还有一个方法Login负责登录。有一次,我想将控制器的名称从 to 更改为User,Account将方法的名称从Loginto更改为Sigin,并且尽可能地替换链接。基本上我有两个问题:

  1. 如果需要更改控制器或方法的名称怎么办,然后在整个项目中查找旧名称并更改为新名称(Startup设置时的视图、代码、类CookieAuthenticationOptions)
  2. 如果项目已经发展到 >=10 个控制器并且在每个控制器中 >=5 个方法HttpGet,并且您无法记住所有这些控制器,您需要调用其中一个,该怎么办。

这是我解决问题的方法

    public static class LinksConstants
        {
            public static class Account
            {
                public const String Controller = "Account";
                public const String SignUpActionName = "SignUp";
                public const String SignInActionName = "SignIn";
                public const String ChangePasswordActionName = "ChangePassword";
                public const String SignOutActionName = "SignOut";
                public static readonly String SignUpLink;
                public static readonly String SignInLink;
                public static readonly String ChangePasswordLink;
                public static readonly String SignOutLink;

                static Account()
                {
                    String mask = "/{0}/{1}";

                    SignUpLink = String.Format(mask , Controller , SignUpActionName);
                    SignInLink = String.Format(mask , Controller , SignInActionName);
                    ChangePasswordLink = String.Format(mask , Controller , ChangePasswordActionName);
                    SignOutLink = String.Format(mask , Controller , SignOutActionName);
                }
            }
        }

        public class AccountController : Controller
        {
            [HttpGet, AllowAnonymous, ActionName(LinksConstants.Account.SignInActionName)]
            public async Task<IActionResult> SignInView([FromServices] SignInManager signInManager ,
                                                        [FromQuery] String returnUrl = null)
            {
                //do work
            }
        }
    <div class="clearfix">
        <a class="float-right"  asp-route-returnUrl="@Model.ReturnUrl" asp-controller=@LinksConstants.Account.Controller asp-action=@LinksConstants.Account.SignUpActionName>Create account</a>
    </div>

您如何在实际项目中解决此类问题?

模型

public sealed class SignInViewModel
    {
        #region Fields
        public static readonly SignInViewModel Empty;

        [Required]
        [DataType(DataType.EmailAddress)]
        [EmailAddress]
        [Display(Name = "Email")]
        public String Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        [Remote(action: LinksConstants.RemoteValidation.ValidationPasswordActionName , controller: LinksConstants.RemoteValidation.Controller)]
        public String Password { get; set; }


        [ScaffoldColumn(false), ValidateNever]
        public String ReturnUrl { get; set; }
        #endregion Fields

        static SignInViewModel()
        {
            Empty = new SignInViewModel()
            {
                ReturnUrl = "/"
            };
        }

        public async Task ValidationOnServer(ModelStateDictionary modelState , UserManager userManager)
        {
            UserEntity user = await userManager.FindByEmailAsync(Email);
            if (user is null || !await userManager.CheckPasswordAsync(user , Password))
            {
                String message = "Invalid email or password";
                modelState.TryAddModelError(nameof(Email) , message);
                modelState.TryAddModelError(nameof(Password) , message);
            }
        }

    }

看法

@using WebContacts.Models.Account;

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

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <form id="account" method="post" asp-route-returnUrl="@Model.ReturnUrl">
        <div class="container py-0 ">
            <div class="row d-flex justify-content-center align-items-center ">
                <div class="col-12 col-md-8 col-lg-6 col-xl-5">
                    <div class="card shadow-2-strong" style="border-radius: 1rem;">
                        <div class="card-body p-5 text-center">

                            <h3 class="mb-5">Sign in</h3>

                            <div class="form-outline mb-4">
                                <input class="form-control form-control-lg" asp-for="@Model.Email" />
                                <label class="form-label" asp-for="@Model.Email"></label>
                                <span class="text-danger" asp-validation-for="@Model.Email"></span>
                            </div>

                            <div class="form-outline mb-4">
                                <input class="form-control form-control-lg" asp-for="@Model.Password" />
                                <label class="form-label" asp-for="@Model.Password"></label>
                                <span class="text-danger" asp-validation-for="@Model.Password"></span>
                            </div>

                            <button class="btn btn-primary btn-lg btn-block" type="submit">Login</button>
                            <div class="clearfix">
                                <a class="float-right"  asp-route-returnUrl="@Model.ReturnUrl" asp-controller=@LinksConstants.Account.Controller asp-action=@LinksConstants.Account.SignUpActionName>Create account</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>


</body>
</html>
c# asp.net-mvc
  • 1 个回答
  • 63 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5