c# - 未在用户注册时设置 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 对数据做了什么,我应该寻找哪些类?
解决方案
您只能更新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,其中包含您希望能够设置的所有属性。
推荐阅读
- python - 使用 pandas 的 dataframe.plot 显示不同的数据
- apache-spark - 使用 spark StreamingKMeans 实时获取集群的中心
- highcharts - 将 Highcharts.js 与 odoo 一起使用
- vba - VBA 中的工厂模式和单例对象
- reactjs - 谷歌图表:自定义范围选择器设计
- python-3.x - yaml.dump 中的转换属性不起作用
- git - 可以从不同的折叠git管理器文件
- python - 如何向不和谐的用户发送私人消息?
- typo3 - TYPO3 9 路由 - 参数被默认值覆盖
- keycloak - Quarkus + keycloak - HTTP/1.1 403 Forbidden