Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC vs. HTML + KnockoutJS + WebAPI [closed]

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:

  1. Multi device support (device detection and template replacement)
  2. Server-side validations of submitted data (not sure as I can also put the validations on WEB API?)
  3. I can still rewrite my URLs even if I'm using static html files (since I'm using ASP.Net MVC anyway).

Can someone help me understand this better? Thanks in advance.

like image 704
Vyas Bharghava Avatar asked Aug 14 '12 19:08

Vyas Bharghava


People also ask

How is Web API different from MVC controller?

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.

What is the difference between Apicontroller and MVC controller?

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}.

What is Knockout JS in asp net?

Knockout. Js (simply KO) is a powerful JavaScript library which allows developers to bind DOM elements with any data model like array, Json etc.


2 Answers

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.

like image 56
Tom W Hall Avatar answered Oct 29 '22 02:10

Tom W Hall


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.

like image 27
David C Avatar answered Oct 29 '22 02:10

David C