1

I'm currently developing an application using ASP.NET MVC5 and Knockout.

For now, the typical architecture is this:

FooController.cs:

public ActionResult GetFoos(){
    //...
    var model = // everything that is needed in the ko viewmodel
    //...
    return View(model);
}

GetFoos.cshtml

<body>
    <!-- potentially a lot of HTML and ko bindings to the view model -->
    <script>
        var fooViewModel = new FooViewModel(@Html.Json(Model));
        ko.applyBindings();
    </script>
</body>

I'm looking for a way to exclude all the view model instantiation from the HTML (i.e. drop the use of Razor), so calls to the server-side controllers would only have to return JSON data instead of full views. That would allow full HTML and JavaScript caching, instead of downloading the view HTML again and again.

Is there a solution that would allow the app to download the necessary HTML script related to a specific action, meaning that this particular script will only have to be downloaded once, as next time we'll need it, it will be cached client-side ? A knockout plugin maybe ? Or any relevant pattern...

  • This is a bit unrelated to your question. But IMHO if you are going that route (which I totally encourage) you should look into dropping the ASP.NET MVC completely and replacing it with something more lightweight like NancyFX or ServiceStack. ASP.NET MVC is more trouble than its worth when you stop using it end-to-end. At least that is my experience from several projects using knockout and mvc together. – wasatz Mar 10 '15 at 6:49
  • @wasatz Thanks for the feedback. I'm afraid dropping MVC is out of the question for now, due to business decisions. But I will definitely take a look at the frameworks you mention. – xlecoustillier Mar 10 '15 at 8:21
  • @x-l-ant Ah, yea, I've been in that situation... Just be prepared that some parts of the MVC framework are not going to work as expected and may need to be disregarded. MVC kind of expects full control all the way, and it gets a bit sad when you rip that away from it. – wasatz Mar 10 '15 at 9:12
  • @wasatz do you have some exemples on the top of your head ? – xlecoustillier Mar 10 '15 at 9:33
  • @x-l-ant The client-side validation will not work. Razor views in general will be less useful since you most likely won't be using any razor at all except for partial views etc. Mainly, you'll probably be using MVC as if it was WebApi - and in that case you may as well use WebApi or something similiar instead since it generally performs better. ;) But as previously stated, business may have objections about that. – wasatz Mar 10 '15 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.