⬆️ ⬇️

Create a web application using VueJS and .NET

This article is a translation of this article . In it, I will tell you how conveniently to use the Vue.js framework when developing an application on ASP.NET MVC

image



Introduction



For the past few months I have been looking at various JavaScript frameworks to integrate them into my MVC projects. As soon as you choose something in the spirit of React, Angular, or Ember for working with .NET, you must install adapter modules, rewrite all routing logic for all controllers. In the end, this affects the entire web application when you want to work side by side with a ready-made working stack. If you are starting a new project, it is advisable to use the Web API for the backend, which will provide the REST interface for the JS framework of your choice, but for existing MVC projects this is not an option.



After some research, I came across Vue.JS and after some experiments I managed to get it to work in conjunction with MVC. Vue.JS is a relatively lightweight framework, so I can add it to the view where I need additional JS features, thereby leaving the rest of the web application untouched. Now, when I use Vue in production, I’m happy to share with you some examples for everyone who shares my problems. My workflow borrows the concept of Migel's Castro [ tyk ] where he used it to integrate AngularJS with MVC.



Getting Started





Now let's use Vue in our submissions





Obviously, putting all your scripts in our views is a guaranteed path to headaches, so for now let's organize our code.



Getting Started



When we have a large application built on VueJS, I divide the project into parts. For each controller, I have a corresponding Vue application, which consists of several Vue components. The one-to-one relationship between the controller and the application makes our code more readable and easy to maintain. Each module of the application will contain only the JavaScript libraries that are needed, and not one large package.



JavaScript packers have improved over the years. Browserify allows you to use the style of node.js modules to work in the browser. We define the dependencies and then Browserify collects them into one small and clean JavaScript file. You include your javascript files using require ("./ your_file.js"); expression. This allows us to use only those libraries that are needed. So now, if we assume that each controller is a container, each of which contains one or more js files. These files will then be placed in a package that will be placed in the folder of our project, which then loads and uses the browser.



I usually follow the following structure. All my Vue code is placed in the ViewModel folder. For each controller that uses Vue, I create a sub folder in the ViewModel folder, and then I call the containers in the main.js file. After that, I use Gulp and Browserify to pack all the files in a package that is stored in the project folder. Views refer to our packages and when the browser requests the page the package is downloaded and launched.



Some practice





Now back to the code





Loading data from the server



With Vue, we can extract and display data that comes from the server.





At the moment, this is enough to use vue.js and MVC, but, like any other frontend frameworks, vue suffers from delays when loading. When a page is requested, JavaScript must be loaded and running. Then the framework makes several more requests to the server to get the data. To prevent this, we must resort to loading animations and other hacks, but since we also use MVC, we can do better and speed up the loading process by using Razor and loading data to present along with the rest of the page.



Initial data load



Many JS frameworks are currently working on different implementations of pre-rendering, but with the .NET stack we can come up with an alternative solution.



A warning! This method is suitable if a small amount of data is involved in the initial load. In other cases, it is better to use pagination or AJAX requests to achieve a smoother page load and reduce delays.



Let's get down to creating. For this we will use the project that we did before.



 public ActionResult Index() { var serverModel = JsonConvert.SerializeObject(new { Name = "Marco", Surname = "Muscat", Description = "Vue data loaded from razor!" }); return View(new SampleModel() { Data = serverModel }); } public class SampleModel { public string Name { get; set; } public string Surname { get; set; } public string Description { get; set; } public string Data { get; set; } } 




Routing



Our project can not be considered complete as there is no routing. More complex applications will require several presentations, since there is too much information on one page. Using Vue with MVC is awesome, since we can load all our views while staying on the same page without a full reload.





The result may seem very good, but that's not all. We still have a problem. If we copy our address with the Vue component (http: // localhost / vue-example / vuerouting / bar) and paste it into a new tab, we get a 404 error because the server is not able to find this route. We need to configure the routing on the server so that it ignores our Vue routes.





Link to the original: tyk .



I hope that the information I provided in this article will help you in creating your projects using VueJS and MVC. If you have comments, questions and suggestions please leave them in the comments.



Thank you all for reading.

Source: https://habr.com/ru/post/324176/



All Articles