Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ServiceStack中没有流畅验证的错误消息_C#_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack_Fluentvalidation - Fatal编程技术网 servicestack,fluentvalidation,C#,servicestack,Fluentvalidation" /> servicestack,fluentvalidation,C#,servicestack,Fluentvalidation" />

C# ServiceStack中没有流畅验证的错误消息

C# ServiceStack中没有流畅验证的错误消息,c#,servicestack,fluentvalidation,C#,servicestack,Fluentvalidation,我刚刚开始熟悉ServiceStack,并进行了FluentValidation。我按照介绍创建了一个小的Hello应用程序 我的问题是,当我尝试验证请求DTO时,没有返回错误消息来描述它是如何失败的,只有一个空的Json对象{} 我自己认为,验证是自动连接到DTO的,因此我不需要编写任何额外的代码 答案可能是明目张胆的,但我看不出来。任何帮助都将不胜感激。我的代码如下: namespace SampleHello2 { [Route("/hello")] [Route("/he

我刚刚开始熟悉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();
        }
    }
}
名称空间SampleHello2
{
[路线(“/你好”)]
[路由(“/hello/{Name}”)]
公共课你好
{
公共字符串名称{get;set;}
}
公共类Hello响应
{
公共字符串结果{get;set;}
}
公共类HelloService:服务
{
公共对象任意(Hello请求)
{
返回新的HelloResponse{Result=“Hello,”+request.Name};
}
}
公共类HelloValidator:AbstractValidator
{
公共HelloValidator()
{
//所有请求的验证规则
RuleFor(r=>r.Name).NotNull().NotEmpty().Equal(“Ian”).WithErrorCode(“ShouldNotBeEmpty”);
RuleFor(r=>r.Name.Length).大于(2);
}
}
公共类全局:System.Web.HttpApplication
{
公共类HelloAppHost:AppHostBase
{
//告诉Service Stack应用程序的名称以及在哪里找到web服务
public HelloAppHost():base(“helloweb服务”,typeof(HelloService.Assembly){}
公共覆盖无效配置(Funq.Container)
{
//启用验证功能
Add(新的ValidationFeature());
容器.注册验证器(类型(HelloValidator).组件);
//注册您的服务使用的任何依赖项,例如:
//Register(newmemorycacheclient());
}
}
//单例初始化应用程序
受保护的无效应用程序\u启动(对象发送方,事件参数e)
{
新HelloAppHost().Init();
}
}
}
另外,非常喜欢使用ServiceStack,这是一个非常棒的项目,谢谢

编辑

例如:

呼叫:
http://localhost:60063/hello/Ian?format=json
返回
{“结果”:“你好,伊恩”}
。 鉴于调用:
http://localhost:60063/hello/I?format=json
返回
{}


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

我找到了答案。这是我的疏忽:

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

将处理下面描述的所有错误处理和验证选项 以相同的方式-序列化到 响应数据以使您的客户端应用程序能够 一般以相同的方式处理所有Web服务错误

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

公共响应status ResponseStatus{get;set;}

另外,当使用
ResponseStatus
属性时,仅当响应DTO遵循命名为
RequestDtoResponse
的约定时,才需要该属性,因为这是DTO服务堆栈将使错误水合为水。如果不存在响应DTO,ServiceStack将使用已具有
ResponseStatus
属性的通用ErrorResponse DTO(即代替您的响应DTO)。