同时使用 WebAPI 和淘汰赛绝对没有坏处。knockout 只是一个客户端绑定库,WebAPI 是一个服务层。
- 您仍然可以创建从 WebAPI 返回的视图模型。
- 您仍然可以使用 MVC 控制器返回您的视图。
- 您可以使用敲除来管理您的客户端视图模型和交互。
一个通用的方法(请阅读很多关于 WebAPI 和 Web API 中的模型绑定)将是。
- MVC 项目 -> 包含一个仅根据请求的操作提供视图的控制器。
- Web API -> 业务逻辑的服务层。
- 模块化 Javascript(使用 requirejs,敲除并阅读有关在 Javascript 中揭示模块模式的信息)来定义您的视图模型(不要将其视为 API 提供的视图模型,这些视图模型还将定义客户端交互、计算的 observables 等)
- Create a Binder JS module that will take in a viewmodel and bind to a DOM.
Things you need to know when you follow this approach.
1. If there are bindings on your Layout and any partials and you call ko.applyBindings(new vm()); you will see errors. Instead, bind to a particular div or element that will act as the container for your viewmodel data.
2. Web API model binders are different than ASP.Net MVC model bindings. Read up on this.
3. Separate out your Javascript files into smaller modules doing specific things and you can handle things better.
If you wish to learn more about this, look up Single Page Application using ASP.Net. The new release of ASP.Net and VS2012 update has SPA templates and lots of examples on using knockout/WebAPI and other libraries such as Ember, Angular .etc.
Hope that helps.
Cheers!