C# 信号器数据注释
C# signalr data annontations
我想编写类似于 Web 中呈现的数据注释的方法验证过程 API。
在网络中api我们可以验证一个对象,例如:
public class Numbers
{
[NumberOne]
public string Number1 { get; set; }
[NumberTwo]
public string Number2 { get; set; }
}
只要我们定义属性 NumberOneAttribute
和 NumberTwoAttribute
就可以了。
不同之处在于网络 api 可以访问 GlobalConfiguration.Configuration.Filters
而 signalr 似乎没有。
有没有按属性验证请求的方法?或者我需要遵循最坏的情况,验证调用方法中的每个输入?
谢谢,
奥里.
在 SignalR 2.2.x 中没有实现此目的的本地方法,但有一个 project on GitHub that that adds a Validation Module in the SignalR pipeline。
基本上,为了使用它,您向管道添加一个新模块:
GlobalHost.HubPipeline.AddModule(new ValidationModule());
然后,您可以为模型的属性使用 [Required]
等属性,然后使用 [Validate]
属性修饰所需的方法。
Note that this is a proof of contept project.
此致!
我想编写类似于 Web 中呈现的数据注释的方法验证过程 API。
在网络中api我们可以验证一个对象,例如:
public class Numbers
{
[NumberOne]
public string Number1 { get; set; }
[NumberTwo]
public string Number2 { get; set; }
}
只要我们定义属性 NumberOneAttribute
和 NumberTwoAttribute
就可以了。
不同之处在于网络 api 可以访问 GlobalConfiguration.Configuration.Filters
而 signalr 似乎没有。
有没有按属性验证请求的方法?或者我需要遵循最坏的情况,验证调用方法中的每个输入?
谢谢, 奥里.
在 SignalR 2.2.x 中没有实现此目的的本地方法,但有一个 project on GitHub that that adds a Validation Module in the SignalR pipeline。
基本上,为了使用它,您向管道添加一个新模块:
GlobalHost.HubPipeline.AddModule(new ValidationModule());
然后,您可以为模型的属性使用 [Required]
等属性,然后使用 [Validate]
属性修饰所需的方法。
Note that this is a proof of contept project.
此致!