I'm wondering why not just have static HTML files in an ASP.Net MVC 4 Web Project that use jQuery+jQuery Templates+KnockoutJS combination consuming REST based (ASP.Net MVC 4 WEB API hosted on Azure & secured using ACS). The Web API can use Entity Framework and return JSON serialized objects that can be retrieved using $.ajax() and bound using KnockoutJS.
What is it that ASP.Net MVC (for the Web pages) provides that adds value to this architecture?
On top of my head, I can think of:
Can someone help me understand this better? Thanks in advance.
The Web API returns the data in various formats, such as JSON, XML and other format based on the accept header of the request. But the MVC returns the data in the JSON format by using JSONResult. The Web API supports content negotiation, self hosting. All these are not supported by the MVC.
An API controller is derived from the API Controller class instead of Controller class. An API controller can not return view instead of they return serialized data whereas an MVC controller can return a view. The Web API controller shows URL examples matching the default route pattern of “{controller}/{action}/{id}.
Knockout. Js (simply KO) is a powerful JavaScript library which allows developers to bind DOM elements with any data model like array, Json etc.
Great question. I'm certainly finding that my MVC / Razor code is becoming less and less as I progress with my Knockout project, but I think I'll always have some aspects of the views which I want to be determined server-side.
Fundamental contextual stuff like whether to render a logged in / logged out panel in a layout page, role related decisions as to what should be accessible, etc. I guess if you were careful enough with your security and implement sufficient guard code on the server when someone actually tries to do something then you could achieve most of that in Knockout, but you'd probably end up with a huge amount of bloat, catering for every possible part of the view.
It probably depends on your application but I think for most web apps there's a fairly common sense division between what should be determined at server render time and what should be done on the client.
If nothing else, you may want links etc in your views to be indexed by search engines. If you pass down, say, your "latest 10 products" in JSON and render them with hyperlinks in a Knockout template, you'd lose out on that.
It is really a question of using the right tool for the job.
From what I can tell after doing development with knockout, it's real power comes from observables and the real time DOM updates. This makes a rich interface in client side applications quicker to create, and easier to manage. However, it is still more time consuming and difficult to implement than working with straight Razor pages. So Knockout JS is an advantage for certain applications where lots of data driven UI updates need to happen "ajaxically", but will be overkill for others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With