0

我即将开始一个新的 Asp.Net MVC 开发,我想做出最好的选择。在我之前的 MVC 开发中,我喜欢一系列的事情,比如:

  • 基于视图模型的视图
  • 客户端验证
  • 远程验证

我在网上阅读了很多关于 Web API 和 Knockout 的内容。我理解每个元素的原理,但在我看来,如果我使用这些元素中的一个(或两个),我将无法从上面列出的优点中受益。

我的新开发将主要是一些基于搜索结果的列表,然后对它进行 CRUD 操作。

我想听听您对在我的新项目中使用 Web API 和/或 Knockout 的意见。我知道这部分是主观的选择,但社区的意见对我来说很重要。也许你已经不得不做出这些选择?

感谢您的意见。

4

1 回答 1

0

同时使用 WebAPI 和淘汰赛绝对没有坏处。knockout 只是一个客户端绑定库,WebAPI 是一个服务层。

  1. 您仍然可以创建从 WebAPI 返回的视图模型。
  2. 您仍然可以使用 MVC 控制器返回您的视图。
  3. 您可以使用敲除来管理您的客户端视图模型和交互。

一个通用的方法(请阅读很多关于 WebAPI 和 Web API 中的模型绑定)将是。

  1. MVC 项目 -> 包含一个仅根据请求的操作提供视图的控制器。
  2. Web API -> 业务逻辑的服务层。
  3. 模块化 Javascript(使用 requirejs,敲除并阅读有关在 Javascript 中揭示模块模式的信息)来定义您的视图模型(不要将其视为 API 提供的视图模型,这些视图模型还将定义客户端交互、计算的 observables 等)
  4. 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!

于 2013-02-28T20:51:29.790 回答