c# - ServiceStack 中的 Fluent Validation 没有错误消息

标签 c# servicestack fluentvalidation

我刚刚开始熟悉 ServiceStack,并开始接触 FluentValidation。我按照介绍创建了一个小型 Hello 应用程序。

我的问题是,当我尝试验证请求 DTO 时,没有返回任何错误消息来描述验证失败的原因,仅返回一个空白 Json 对象{}

我自己,我认为验证是自动连接到 DTO 的,所以我不需要编写任何额外的代码。

答案可能很明显,但我看不到。任何帮助将不胜感激。我的代码如下:

namespace SampleHello2
{
    [Route("/hello")]
    [Route("/hello/{Name}")]
    public class Hello
    {
        public string Name { get; set; }
    }

    public class HelloResponse
    {
        public string Result { get; set; }
    }


    public class HelloService : Service
    {
        public object Any(Hello request)
        {
            return new HelloResponse { Result = "Hello, " + request.Name };
        }
    }

    public class HelloValidator : AbstractValidator<Hello>
    {
        public HelloValidator()
        {
            //Validation rules for all requests
            RuleFor(r => r.Name).NotNull().NotEmpty().Equal("Ian").WithErrorCode("ShouldNotBeEmpty");
            RuleFor(r => r.Name.Length).GreaterThan(2);
        }
    }

    public class Global : System.Web.HttpApplication
    {
        public class HelloAppHost : AppHostBase
        {
            //Tell Service Stack the name of your application and where to find your web services
            public HelloAppHost() : base("Hello Web Services", typeof(HelloService).Assembly) { }

            public override void Configure(Funq.Container container)
            {
                //Enable the validation feature
                Plugins.Add(new ValidationFeature());
                container.RegisterValidators(typeof(HelloValidator).Assembly);
                //register any dependencies your services use, e.g:
                //  container.Register<ICacheClient>(new MemoryCacheClient());
            }
        }

        //Initialize your application singleton
        protected void Application_Start(object sender, EventArgs e)
        {
            new HelloAppHost().Init();
        }
    }
}

附注真的很喜欢使用 ServiceStack,这确实是一个很棒的项目,所以谢谢。

编辑

例如:

调用:http://localhost:60063/hello/Ian?format=json 返回{"Result":"Hello, Ian"}。 而调用:http://localhost:60063/hello/I?format=json 返回 {}

第二个调用返回 {},我期望自动生成错误消息。

最佳答案

我找到了答案。这对我来说是一个忽视:

这是在文档中的,我忽略了它:

All Error handling and validation options described below are treated in the same way - serialized into the ResponseStatus property of your Response DTO making it possible for your clients applications to generically treat all Web Service Errors in the same way.

因此,我的代码中缺少的就是将以下行添加到 HelloResponse 类中。

public ResponseStatus ResponseStatus { get; set; }

关于c# - ServiceStack 中的 Fluent Validation 没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15160053/

相关文章:

c# - Autofac/流利验证 : No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested

c# - c#中的哈希字符串

c# - 使用客户端证书的 WCF 传输安全

c# - 访问包含在匿名类型或对象类中的字段

servicestack - 为什么 ServiceStack 会给 DTO 带来路由问题的负担?

ServiceStack:在与 Google 进行身份验证时从身份验证 session 中获取电子邮件

c# - ServiceStack 中的直通身份验证

c# - 使用 Ninject 的 MVC 5 流畅验证

c# - ResourceManager 返回文件名

c# - 如何通过验证和可选的请求上下文直接从 C# 代码调用 servicestack 服务