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

查看:1282
本文介绍了启动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 文件如下:

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

host.Run();

我希望默认Web浏览器自动打开,以避免用户不得不重复打开浏览器并手动输入 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() (或在2.x上 await StartAsync )而不是 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");

编辑:我创建了门票和一位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天全站免登陆