Marco
Marco

Reputation: 2473

ASP.NET MVC, Knockout, Web API - Formatting Concerns

I am using ASP.NET MVC / WebAPI and Knockout to generate my views. I am trying to figure out where I should handle formatting, url generation, etc (I would normally do in my controller and return a view model).

Is is an ok practice to have my WebAPI return view models with preformatted data or should I leave that to the caller?

Please note the API is only used by my application

Upvotes: 1

Views: 294

Answers (1)

WestDiscGolf
WestDiscGolf

Reputation: 4108

What I'd suggest is the following:

  • WebApi controller actions provide / consume data (json data) - Http verb actions

  • On your knockout viewmodel it contains methods to get / save etc the json data which is called on document ready to populate the data

  • The default mark-up is created by your MVC view returned from the controller on initial load and then any further can be done dynamically (either inline or templates; native or knockout)

In my opinion the WebApi should not return the full view model. It should only return the data consumed by your view model. The api points should not be implementation specific and allow for knockout consumption as well any other client.

HTH

Upvotes: 2

Related Questions