Model validation with NancyFX

那年仲夏 提交于 2019-12-10 14:19:58

问题


I'm really get used with ASP.NET MVC approach: annotate model with corresponding attributes, MVC does validate it and updates ModelState.Errors, ModelState is available on View, so it is possible to show errors to user.

I haven't found information on wiki how exactly it should be done for NancyFX. In sources I can see that Nancy trying to use Rules, but how to apply correctly and show failures on Views?

EDIT: Question is relevant to Nancy 0.9, according to @TheCodeJunkie 0.10 will apply changes for that.


回答1:


In the current tagged release (0.9) we do not have any built in support for model validation. However with the pending (most likely this weekend) 0.10 release we are adding support for model validation and will be shipping nuget packages for using either data annotations or fluent validation.

The master branch contains a demo project that shows how to use them https://github.com/NancyFx/Nancy/tree/master/src/Nancy.Demo.Validation

Of course the validation stuff is extensible and you can provide your own implementations for the validation framework of your own choice. The Nancy core only contains a small set of extensibility points and then the actual implementations are shipped as separate nugets.

The 0.10 release will contain the first building blocks for allowing the creation of client-side validation based on your model. However, the 0.10 release will not contain the actual client side validation stuff, but the hooks to extract the validation information is there as shown in this sample module

https://github.com/NancyFx/Nancy/blob/master/src/Nancy.Demo.Validation/ProductsModule.cs#L16

Hope this helps!



来源:https://stackoverflow.com/questions/9225316/model-validation-with-nancyfx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!