Convert your ASP.NET Core to desktop application in a few steps.
- Create a ASP.NET Core Minimal API project.
dotnet add package Drk.AspNetCore.MinimalApiKit
to add reference.- Add
<script src="/sse.js"></script>
in your web page HTML. - In Program.cs, use Drk.AspNetCore.MinimalApiKit namespace and change
app.Run()
toapp.RunAsDesktopTool()
.
The default browser will launch as soon as the application start and navigate to its home webpage. After closing all browser window/tab of the web, the application will exit automatically soon.
You can find the example project in src/RunAsDesktopToolDemo
folder.
程式說明:Electron.NET 太笨重?用 ASP.NET Core Minimal API 寫桌面小工具的快速做法
- Create a ASP.NET Core Minimal API project.
- Change OutputType and TargetFramework to WinExe and net6.0-windows/net7.0-windows, add UseWindowsFroms true if you want to use Windows Form control or MessageBox:
<PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net6.0-windows</TargetFramework> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup>
dotnet add package Drk.AspNetCore.MinimalApiKit
to add reference.- In Program.cs, use Drk.AspNetCore.MinimalApiKit namespace and change
app.Run()
toapp.RunWithNotifyIcon(new NotifyIconOptions {...})
. The NotifyIconOptions argument is required to define notify icon image, tooltip and context menu items.
Example:app.RunWithNotifyIcon(new NotifyIconOptions { IconStream = typeof(Program).Assembly.GetManifestResourceStream($"AsNotifyIconDemo.App.ico"), ToolTip = "NotifyIcon ToolTip", MenuItems = { NotifyIconOptions.CreateLaunchBrowserMenuItem("Launch Browser", (webBaseUrl) => webBaseUrl), NotifyIconOptions.CreateMenuSeparator(), NotifyIconOptions.CreateActionMenuItem("Say Hello", (state) => { System.Windows.Forms.MessageBox.Show("Hello, World!"); }) } });
You can find the example project in src/RunWithNotifyIconDemo
folder.