Electron.NET provides a wrapper around Electron with an ASP.NET Core MVC application in the mix. Here is the reason why this was done from the creates of the project.
Well… there are lots of different approaches how to get a X-plat desktop app running. We thought it would be nice for .NET devs to use the ASP.NET Core environment and just embed it inside a pretty robust X-plat enviroment called Electron. Porting Electron to .NET is not a goal of this project, at least we don’t have any clue how to do it. We just combine ASP.NET Core & Electron.
Seems only fitting to use VS Code for this post since it is built using the same base library. From the command line create a new ASP.NET Core MVC application using the following command.
dotnet new mvc
Make note that it is important at this time that you use the MVC template when attempting this and not a Razor Page application. This is more of a warning if you are using Visual Studio to do your project creation and not the CLI command above.
Next, we need to reference the ElectronNET.API NuGet package which can be added using the following command.
dotnet add package ElectronNet.API
Then, open the csproj and add a reference for the Electron.NET CLI tool which should match the following.
<ItemGroup> <DotNetCliToolReference Include="ElectronNET.CLI" Version="0.0.9" /> </ItemGroup>
After that make sure and restore packages using the following command.
Open Program.cs and add UseElectron(args) to the WebHost builder chain.
WebHost.CreateDefaultBuilder(args) .UseElectron(args) .UseStartup<Startup>() .Build();
Net, open Startup.cs and add the following line to the bottom of the Configure function. This is what will open the Electron window on startup.
Task.Run(async () => await Electron.WindowManager.CreateWindowAsync());
Finally, run the following from the command prompt to get the project ready to run under Electron. This should only need to be done once.
dotnet electronize init
Run the application
At this point, the application is ready to go. The neat part is you can just hit F5 and it will run like any normal ASP.NET Core application (this would change when you get into Electron specific calls I’m sure) or if you run the following command it will run inside of the Electron shell.
dotnet electronize start
It was surprisingly simple to get a new application up and running, of course so far the app isn’t anything other than a web site hosted in Electron. My plan is to take this base application and create my normal basic contacts application, which will be the next post. From there I may look into layering in some Electron features.
The completed code can be found here.
Also published on Medium.