c# - ServiceStack 验证不显示消息

标签 c# servicestack

我正在使用 ServiceStack(带有新 API)并尝试验证 DTO。刚刚创建了一些简单的代码来模拟验证,但它显然没有触发,或者至少没有按预期显示响应错误。我的代码如下:

DTO:

[Route("/users/login")]
public class UserLogin
{
    public string Email { get; set; }
    public string Password { get; set; }
}

验证器本身:

public class UserLoginValidator : AbstractValidator<UserLogin>
{
    public UserLoginValidator()
    {
        RuleSet(ApplyTo.Get, () =>
        {
            RuleFor(x => x.Email).NotEmpty().WithMessage("Please enter your e-mail.");
            RuleFor(x => x.Email).EmailAddress().WithMessage("Invalid e-mail.");
            RuleFor(x => x.Password).NotEmpty().WithMessage("Please enter your password.");
        });
    }
}

在主机中配置验证:

Plugins.Add(new ValidationFeature());
container.RegisterValidators(typeof(UserLoginValidator).Assembly);

和服务:

public class LoginService : Service
{   
    public object Get(UserLogin request)
    {
        var response = new { SessionId = Guid.NewGuid() };
        return response;
    }
}

是否需要进行任何其他配置或调整才能使其正常工作?

谢谢!

最佳答案

来自documentation

Note: The response DTO must follow the {Request DTO}Response naming convention and has to be in the same namespace as the request DTO!

尝试为响应创建一个类

public class UserLoginResponse
{
    public UserLogin Result { get; set; }
}

然后返回

public class LoginService : Service
{   
    public object Get(UserLogin request)
    {
        var response = new UserLoginResponse { Result = request };
        return response;
    }
}

关于c# - ServiceStack 验证不显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13869058/

相关文章:

c# - 将数字转换为单词

javascript - 将 ViewBag 数据加载到 Chart.js 多图表中

c# - 按大写字母拆分字符串(不包括带连字符的单词)

c# - OrderBy 和 List 与 IOrderedEnumerable

c# - 使用元组作为接口(interface)通用的显式接口(interface)实现不起作用

redis - 如何使用 ServiceStack Redis 客户端获取多个 HashSet

servicestack - 向 ServiceStack 身份验证提供程序添加功能

redis - 如何将 requestDto 推送到 Redis 并一直保存到它被读取?

javascript - Nservicekit反序列化

memcached - ServiceStack ICacheClient 增量不起作用