启动ASP后如何启动web浏览器.NET核心应用程序
本文关键字:启动 web 浏览器 NET 应用程序 核心 ASP 何启动 | 更新日期: 2023-09-27 17:58:37
我有一个ASP。NET核心应用程序,该应用程序将被多个用户用作客户端。换句话说,它不会托管在中央服务器上,他们将在需要使用应用程序的任何时候运行发布的可执行文件。
在Program.cs
文件中有以下内容:
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
我希望默认的web浏览器自动打开,以避免用户必须打开浏览器并手动输入http://localhost:5000
地址的冗余步骤。
实现这一目标的最佳方式是什么?在调用Run()
之后调用Program.Start
将不起作用,因为Run
阻塞了线程。
这里有两个不同的问题:
线程阻塞
host.Run()
确实阻塞了主线程。因此,使用host.Start()
(或2.x上的await StartAsync
)而不是host.Run()
。
如何启动web浏览器
如果您使用的是ASP。NET Core over。NET Framework 4.x,微软说你可以直接使用:
Process.Start("http://localhost:5000");
但如果你的目标是多平台。NET核心,上面的行将失败。没有一个使用.NET Standard
的解决方案适用于每个平台。Windows唯一的解决方案是:
System.Diagnostics.Process.Start("cmd", "/C start http://google.com");
编辑:我创建了一个票证,一位微软开发人员回答说,从今天起,如果你想要一个多平台版本,你应该手动完成,比如:
public static void OpenBrowser(string url)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); // Works ok on windows
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url); // Works ok on linux
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url); // Not tested
}
else
{
...
}
}
现在一起:
using System.Threading;
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Start();
OpenBrowser("http://localhost:5000/");
}
public static void OpenBrowser(string url)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
// throw
}
}
}
这里的另一个选项是解析Startup.Configure
中的IApplicationLifetime
对象并在ApplicationStarted
上注册回调。该事件是在主机旋转并正在侦听时触发的。
public void Configure(IApplicationBuilder app, IApplicationLifetime appLifetime)
{
appLifetime.ApplicationStarted.Register(() => OpenBrowser(
app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First()));
}
private static void OpenBrowser(string url)
{
Process.Start(
new ProcessStartInfo("cmd", $"/c start {url}")
{
CreateNoWindow = true
});
}
接受的答案是好的,但由于没有阻塞,程序将立即结束,停止服务器。以下是根据杰拉多和伊凡的回答改编的版本。
它将创建服务器,在服务器开始侦听时启动浏览器,并阻止直到服务器结束:
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System.Diagnostics;
using Microsoft.AspNetCore.Builder;
using static System.Runtime.InteropServices.RuntimeInformation;
using static System.Runtime.InteropServices.OSPlatform;
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:54321/";
using (var server = CreateServer(args, url))
{
StartBrowserWhenServerStarts(server, url);
server.Run(); //blocks
}
}
/// <summary>
/// Create the kestrel server, but don't start it
/// </summary>
private static IWebHost CreateServer(string[] args, string url) => WebHost
.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls(url)
.Build();
/// <summary>
/// Register a browser to launch when the server is listening
/// </summary>
private static void StartBrowserWhenServerStarts(IWebHost server, string url)
{
var serverLifetime = server.Services.GetService(typeof(IApplicationLifetime)) as IApplicationLifetime;
serverLifetime.ApplicationStarted.Register(() =>
{
var browser =
IsOSPlatform(Windows) ? new ProcessStartInfo("cmd", $"/c start {url}") :
IsOSPlatform(OSX) ? new ProcessStartInfo("open", url) :
new ProcessStartInfo("xdg-open", url); //linux, unix-like
Process.Start(browser);
});
}
}
您可以在host.Run()
之前生成web浏览器进程。这适用于Chrome,可能还有其他浏览器:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
var browserExecutable = "C:''Program Files (x86)''Google''Chrome''Application''chrome.exe";
Process.Start(browserExecutable, "http://localhost:5000");
host.Run();
}
在Chrome的情况下,新窗口将等待服务器启动,然后连接并显示应用程序。
根据系统的配置方式,您可以执行Process.Start("http://localhost:5000")
来启动默认浏览器,而不是硬编码可执行文件的路径。出于某种原因,这对我不起作用。您也可以从注册表或配置文件中提取浏览器路径。