用户注册时未设置 BirthDate 和 BirthDateRaw

BirthDate and BirthDateRaw not set on user registration

我有一个表单通过将数据发送到默认路由 ~/api/register 来处理用户注册,但它不适用于 BirthDate 和 BirthDateRaw(分别映射为 DateTime?string 在 ServiceStack 的 UserAuth class 中。对于这两个字段,我的用户 table 中的相应属性仍然是 NULL(我使用的是 SQLite),我无法理解原因。

这是我的代码的基本示例。

<form method="post" action="~/api/register">
    <label for="Input_BirthDate">Your birth date</label>
    <input type="date" id="BirthDate">

    <label for="Input_BirthDateRaw">Your birth date in raw text</label>
    <input type="text" id="BirthDateRaw">

    <label for="Input_Email">Your email address</label>
    <input type="email" id="Email">

    <label for="Input_Password">Your password</label>
    <input type="password" id="Password">

    <input type="submit" value="Create new user">
</form>

需要说明的是,我实际上并没有以两种格式询问用户的出生日期,但我将它们包括在内以展示我到目前为止所做的尝试。 另外,请注意,我已经通过添加一些额外的属性扩展了默认的 UserAuth class,但是 我使用的是默认的注册服务。

如果我将表单传递给 JS 函数进行提交,快速 console.log() 显示输入没有异常,所以问题一定出在服务器端;有人可以就此事提供任何见解吗?

另一方面,如果我想放置一个断点以在调试器中清楚地看到幕后发生的事情以及数据到达服务器后 ServiceStack 正在对数据做什么,classes我应该找吗?

您只能更新 Register DTO 上的属性:

public class Register : IPost, IReturn<RegisterResponse>, IMeta
{
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
    public bool? AutoLogin { get; set; }
    public string Continue { get; set; }
    public string ErrorView { get; set; }
    public Dictionary<string, string> Meta { get; set; }
}

如果您需要捕获更多信息,您需要添加自定义 OnRegistered() AuthEvent 以填充来自 IRequest 的其他输入并在 AuthRepository 中更新它,例如:

public class CustomUserAuth : AuthUserSession
{
    public override void OnRegistered(IRequest req, IAuthSession session, 
        IServiceBase authService)
    {
        var authRepo = HostContext.AppHost.GetAuthRepository(req);
        using (authRepo as IDisposable)
        {
            var userAuth = (AppUser)authRepo.GetUserAuth(session.UserAuthId);
            userAuth.BirthDateRaw = request.FormData["BirthDateRaw"];
            authRepo.SaveUserAuth(userAuth);
        }
    }
}

或者让它调用您自己的服务(例如在注册后),或者将您自己的 custom Register Service 与包含您希望能够设置的所有属性的自定义注册 DTO 一起使用。