Making ASP.NET Core MVC Apps into Single Page Apps using AppRun
Tags: spa, aspnet, mvc, webdev
Published: 2018-08-17 13:55:01

---
title: Making ASP.NET Core MVC Apps into Single Page Apps using AppRun
published: true
description: 
tags: SPA, ASP.NET, MVC, WebDev
---

_A single-page application (SPA) is a web application or website that interacts with the user by dynamically rewriting the current page rather than loading entire new pages from a server. This approach avoids interruption of the user experience between successive pages, making the application behave more like a desktop application.
— From Wikipedia_

If you have invested and built applications using the server-side technology, such as ASP.NET MVC and ASP.NET Core MVC, you probably know that it is hard to make the server-side rendered applications into the single-page applications. You will need to change the server-side routes to be the Web API that returns data in JSON format. You will also need to develop the client-side templates and state management to handle the JSON data. It is like re-develop the application. After you’ve done the work, you have a single-page application. However, you will find out that now you are missing the server-side rendering capability to improve the first loading time and to support SEO.

I give you good news. Use [AppRun](https://github.com/yysun/apprun).

_AppRun is a 3K library for developing applications using the elm inspired architecture, events, and components._

AppRun can make the server-side rendered apps into the single-page apps instantly, just like sprinkling a little magic.

#How does it work?
When the first time users visit the pages of the app, the app returns the server-side rendered HTML. 

![App returns HTML](https://cdn-images-1.medium.com/max/1600/1*kRnYJayBjJOyrA-ewjcLeQ.png)

Once the app is loaded, the client-side AppRun app switches the app into the single-page app mode. When users click the page links, the app returns the virtual DOM from the server. The client-side AppRun renders the virtual DOM to the real DOM.

![App returns Virtual DOM](https://cdn-images-1.medium.com/max/1600/1*1ZtgK-R4YDb8P4ahLq60Hg.png)

#How to Make it?
First, we include the [Virtual DOM filter attribute](https://github.com/yysun/apprun-ssr-aspnet-core/blob/master/WebApplication4/VirtualDomAttribute.cs) in the project. Then, we apply the filter attribute to the server-side routes.

![Apply the Virtual DOM Filter](https://cdn-images-1.medium.com/max/1600/1*v2eU26XAcxChFXmYNaK5cw.png)
Then, we add a client-side AppRun app to the _Layout.cshtml.

![The Client-Side AppRun App](https://cdn-images-1.medium.com/max/1600/1*_jpY9GrOqqs3zo3mnEYeyw.png)

Finally, we wrap the page container with a div element, &lt;div id=”apprun-app”/&gt; for the client-side app to render the page.

![Element for the AppRun App](https://cdn-images-1.medium.com/max/1600/1*WsqRxJPbSSisP_QDNDnNxg.png)
That’s all the magic you need! 

To summarize, the server-side virtual DOM attribute does content negotiation. When the requests are JSON requests, it converts the pages into virtual DOM. Otherwise, it returns the HTML of the pages. The client-side app switches the app into the SPA mode by changing the behavior of the navigation menus from hyperlinks to the JSON requests. When the server returns the virtual DOM, the client-side app diffs and renders the virtual DOM to the real DOM. 

It is a straightforward solution to building SPAs out of server-side MVC apps. There are no needs of the data API or client-side templates. Best of all, it is non-destructive to your existing application logic.

#Source Code

Download and try it yourself. https://github.com/yysun/apprun-ssr-aspnet-core

Also, if you have the non-Core version of ASP.NET MVC 5.x applications, you can do the same. https://github.com/yysun/apprun-ssr-aspnet

I plan to create a NuGet package to make it even easier to use, but I cannot wait to share the idea to those might have interests earlier.

#Conclusion
AppRun is a powerful client-side framework. It can instantly make server-rendered applications into the single-page applications. 

The same idea applies to the applications using other server-side technologies. I have successfully implemented the concept to Wordpress applications. I am working on express.js (Node.js) and Laravel (PHP) examples. I will write posts about them in the future. Stay tuned.

For more information about AppRun, please visit https://github.com/yysun/apprun.
	
Have fun!