I wanted to talk about a trend I’ve been seeing as a web developer lately. And by web developer, I mean strictly in the .NET space, meaning technologies like ASP.NET MVC and to a lesser extent, Web Forms. So what is this trend? I’m glad you asked.
Even now, you don’t even have to hit the database for any data when a page is requested. You can essentially send down a skeleton of HTML, and then when the browser gets the response, make an AJAX request to a service endpoint (WCF, old school ASMX, .NET Web API, choose your poison) to get a chunk of JSON, then construct the HTML you need from that. And I’ve been leaning more and more towards that method of web development. Why? Can’t really say for sure, at least not right now. It just feels right to do it that way. It makes sense to me.
It has prompted me to ask this question: Is it better to send data with the initial request, or make the request after the page has been loaded by the browser? That might not make sense, let me clarify a bit.
Let’s say you have an MVC project. You have a controller with an Index() action. At this point, you can do one of two things:
- You can go to the database to get whatever data you need, create a view model (if necessary), and provide a view name which processes the model to create the final chunk of HTML to send to the browser.
So are there benefits to doing one over the other? I haven’t explored that question much and don’t have any insight/answers at this point in time. But I thought it would make for good discussion. I would like to get some feedback. Feel free to leave a comment with your thoughts.