Why does the model binder need an empty constructor

前端 未结 4 1676
闹比i
闹比i 2020-12-21 05:21

I need some help with some fundamentals here...

I have this controller that serves up my view with an instance of a class (at least that\'s how I think it works). So

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 05:50

    There is no direct connection between model you pass to view and model that you receive in request. In ultimate case the code for initial request and response will run in different instance of IIS or even different machines.

    So when request come back ASP.Net MVC need to recreate all objects (controller, model,...). Having default constructor allows run-time to create new object without knowledge of particular arguments to your custom constructor.

    Side note: Similar reconstruction for constructor exist for generics where you can only specify where T:new() for default constructor.

提交回复
热议问题