Recently I have been working on a large ASP.Net Core web application that also needs to have pluggable components so the main application can be deployed with or without them. It is possible to add a reference to another project in the Visual Studio Solution and have what are called Areas, but I don’t want this dependency so I went for Application Parts.

An application part is a .net assembly that can be used in the main application through reflection, you just need to tell the Startup class to look for them and include them with any other services.

This can be done through the ConfigureServices method in startup.cs

The ConfigureApplicationParts method is where we search for a Plugins directory and iterate through any libraries we find.

This Plugins directory must be in the bin directory of the main application like this:-

image

For this example I am using a separate ASP.Net Core 3.0 MVC application called Plugin which comprises of Controllers, ViewModels, Views, CSS and JavaScript files. I want them all packaged up and dropped into the Plugins directory of the main application.

When this is done, the Plugin project creates a plugin.dll and a plugin.View.dll which is then loaded within the ConfigureApplicationParts method and depending on its name will be added to the ApplicationParts collection. As can be seen in this code above, it is expecting compiled Razor views. Lets go over this and see how we can embed these resources.

In the Plugin project, right click the project in Solution Explorer and choose Edit Project File

image

Make sure there is an element for ItemGroup and add the views as an embedded resource.

The **\* tells it to include all subdirectories.

So what about JavaScript and styles? Right click on each file in Solution Explorer and choose Properties. Under Build Action, select Embedded resource.

image

This can also be done when editing the project file by removing and adding the resources like this:-

But occasionally I have had issues with this method and the project has refused to load after saving.

Out of the box, a ASP.Net Core 3.0 MVC application doesn’t have a styles section in the Layout file, so add one after the other CSS file references in the main application like this:-

Going back to the Plugin project, we need to add these styles and scripts to the view. To do this we need to load itself as an assembly and find any embedded resources, of which we know there should be one each for the CSS and JavaScript from earlier. I have put it in the Index view of a Plugin controller, but it would be better refactored out and injected in. For simplicity sake:-

Where the PageViewModel is:-

Again, feel free to refactor, but this will get you up and running.

The name of the resource by default will be the namespace of the application appended with the directory structure of the resource, but if you are unsure of what to insert into GetManifestResourceStream(), you can set the Plugin project as the startup project, debug and look at:-

So now the PageViewModel contains both resources as strings, so in the View add the model and the sections for scripts and styles.

Again for simplicity sake I have this as the plugin.css style:-

This as the plugin.js script:-

Build the Plugin project and drop the Plugin.dll and Plugin.Views.dll into the main application plugin directory mentioned earlier. If you already have the main application running in IIS Express, you will need to stop it by right clicking the icon and choosing Exit.

Finally set the main application as the startup project and run it. As I have my plugin controller route set to /Plugin/Index, you can navigate to this path and the plugin will display its very simple message. Also with the browser developer tools open you can see the console.log message as well.

image

You can find this solution on GitHub here:- https://github.com/asteropesystems/Application-Parts-Sample-Solution

Happy coding.

What We Are Trying to Achieve

As a side project I am in the middle of porting a website from Angular.js to Vue.js. Why you may ask, well it is not a complicated site and even though I am proficient with AngularJS, I saw the jump to Angular too heavy and I don’t need all the functionality or complexity that introduces. I have worked with Vue.js before and find it a superb framework that allows you to get a site running quickly with just the basics and only include the parts of the framework you need. It is easy enough to come back and put more functionality in such as routing, state management later on.

So as part of this work I have large chunks of TypeScript that interact with jQuery UI that I need to bring over. I know I could write or find on GitHub some of this functionality, but this is supposed to be a quick project and I haven’t got the time. I just need to get Vue to interact with jQuery UI written in TypeScript. Should be easy…and it is.

Create the Component

Firstly a new TypeScript file is needed to place the jQuery UI functionality into. I am going to use the DatePicker in this example, but the process is pretty much the same with other widgets as well.

For TypeScript to understand what Vue is, you need to import Vue, then you extend the Vue instance adding the template and mounted functions. The mounted function is a lifecycle hook you can use for when the Vue instance is being instantiated.It allows you to start to interact with the DOM or Virtual DOM in the case of Vue.

In the above example you simply use this.$el to get jQuery to find the element as you would any other jQuery UI code. Looking at the DatePicker API, there is a onSelect function that gets called when the date is changed. To get Vue to use this date object, we need to emit it using self.$emit and a kebab case property that matches a property in the HTML.

In the HTML (actually Razor syntax in this case) we add the element like this:-

Where the same kebab cased property is used to call a function called calculateJulianDate. I have to use two @ symbols here other wise Razor will think it is a segment of Razor code.

Import Into Your Vue Page

So now we come to our page code. Generally I set out my Vue code so all I need to call is run the WebPack CLI command and it will pick up my app.js file (generated from app.ts).

This app.js file simply lists out all the JavaScript files I want to compile into the one script. So I have for example:-

Which includes both page and datepicker scripts.

So at the top of page.ts I again need to do an import, this time for DatePicker which works because we export it from the datePicker.ts file earlier.

We also need to extend the Vue instance similar to what we did earlier and register the component like this:-

Now we also need to add the actual function we specified earlier as a kebab case property, in this case I am calculating the Julian Date from the date object passed in so we use the Vue methods properties like this:-

I have left out the actual function code as it doesn’t add anything to this example, but the take home is it sets a data property which is then bound to the HTML like this:-

Happy coding

HTTP Endpoint Monitoring for both Windows and Android
WebUp App