如何在启动 ASP.NET Core 应用程序后启动 Web 浏览器? [英] How do I launch the web browser after starting my ASP.NET Core application?

查看:132
本文介绍了如何在启动 ASP.NET Core 应用程序后启动 Web 浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET Core 应用程序,它将被多个用户用作客户端.换句话说,它不会托管在中央服务器上,他们将在需要使用应用程序的任何时候运行已发布的可执行文件.

I have a ASP.NET Core application that will be used as a client by multiple users. In other words, it will not be hosted on a central server and they will run the published executable anytime they need to use the application.

Program.cs 文件中有以下内容:

In the Program.cs file there's the following:

var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .Build();

host.Run();

我希望默认的网络浏览器自动打开,以避免用户必须打开浏览器并手动输入http://localhost:5000地址的多余步骤.

I would like the default web browser to automatically open to avoid a redundant step of the user having to open the browser and manually put in the http://localhost:5000 address.

实现这一目标的最佳方法是什么?在调用 Run() 之后调用 Program.Start 将不起作用,因为 Run 阻塞了线程.

What would be the best way to achieve this? Calling Program.Start after the call to Run() won't work because Run blocks the thread.

推荐答案

这里有两个不同的问题:

You have two different problems here:

线程阻塞

host.Run() 确实阻塞了主线程.因此,使用 host.Start()(或 await StartAsync 在 2.x 上)而不是 host.Run().

host.Run() indeed blocks the main thread. So, use host.Start() (or await StartAsync on 2.x) instead of host.Run().

如何启动网络浏览器

如果您在 .NET Framework 4.x 上使用 ASP.NET Core,Microsoft 你可以只使用:

If you are using ASP.NET Core over .NET Framework 4.x, Microsoft says you can just use:

Process.Start("http://localhost:5000");

但是如果您的目标是多平台 .NET Core,上面的代码行将会失败.没有使用 .NET Standard 的单一解决方案适用于所有平台.仅适用于 Windows 的解决方案是:

But if you are targeting multiplatform .NET Core, the above line will fail. There is no single solution using .NET Standard that works on every platform. The Windows-only solution is:

System.Diagnostics.Process.Start("cmd", "/C start http://google.com");

我创建了一个 ticket 并且一位 MS 开发人员回答了这个问题——今天,如果你想要一个多平台版本,你应该手动完成,比如:

I created a ticket and a MS dev answered that as-of-today, if you want a multi platform version you should do it manually, like:

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 
        }
    }
}

这篇关于如何在启动 ASP.NET Core 应用程序后启动 Web 浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆