【问题标题】:Nancy Model Binding Required Attribute Validation Not WorkingNancy 模型绑定所需的属性验证不起作用
【发布时间】:2017-07-26 18:43:45
【问题描述】:

我正在通过路由 url 使用 NancyFx 模型绑定,并尝试为所需属性设置一些验证。类如下:

public class Query
{
    [Required]
    public string ClientId { get; set; }
    public List<string> Customers { get; set; }
}

我的路线如下:

Get["/test?customers=c1,c2"] = args =>
        {
            var query = new Query(); // A

            try
            {
                query = this.Bind<Query>(); // B
            }
            catch (ModelBindingException ex)
            {
                throw ex;
            }

            return db.Execute(Query);
        };
    }

在 A 处,我预计会有某种异常,因为 ClientId 是必需的,但在初始化新 Query 时它为空,但什么都没有。

失败了,在 B,我希望在尝试绑定 Query 对象时会出现某种错误。查看调试器,查询对象的 Customers 属性正确地具有“c1,c2”的预期值。但是查询对象中的ClientId为null,并没有报错。我想知道基于需要 ClientId 的事实我能做些什么来触发异常。

【问题讨论】:

    标签: c# .net model-binding nancy requiredfieldvalidator


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 2021-12-01
      • 2013-07-24
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多