首页 > 解决方案 > 未在用户注册时设置 BirthDate 和 BirthDateRaw

问题描述

我有一个通过将数据发送到默认路由来处理用户注册的表单~/api/register,但它不适用于 BirthDate 和 BirthDateRaw (分别映射为DateTime?string在 ServiceStack 的 UserAuth 类中)。对于这两个字段,我的用户表中的相应属性仍然存在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 类,但我使用的是默认注册服务。

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

另一方面,如果我想放置一个断点以便在调试器中清楚地看到幕后发生了什么,以及一旦数据到达服务器,ServiceStack 对数据做了什么,我应该寻找哪些类?

标签: c#sqliteservicestackormlite-servicestack

解决方案


您只能更新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);
        }
    }
}

或者让它调用您自己的服务(例如在注册之后),或者使用您自己的自定义注册服务和自定义注册 DTO,其中包含您希望能够设置的所有属性。


推荐阅读