Is it possible to have a non-public parameterless constructor that can be used for model binding?

前端 未结 2 1054
醉话见心
醉话见心 2021-01-24 20:39

I have a following POCO class. I don not want the parameterless constructor to be public.

public class FileDownloadRequest
    {
       //public FileDownloadRequ         


        
2条回答
  •  花落未央
    2021-01-24 21:05

    Also, while Patrick's answer is great and shows how to do it (in scenarios where this much effort really makes sense), I just add something I've noticed in another SO post.

    Basically, mark the parameterless constructor as [Obsolete("Comment to indicate its for binding only")] and that will prevent others from accidentally calling the parameterless one. (Thus showing explicitly which properties are required by the request object)

提交回复
热议问题