How do I load an local ASP.NET Core website into WebView2?

279 Views Asked by At

From what I can see, I need to run an exe file, somehow guess the localhost port number and use http://localhost:<port_number> URL in the WebView2 control.

Is there any more straightforward approach?

2

There are 2 best solutions below

0
On

You could try below steps:

1)Create ASP.NET Core website

2)Make sure the WebView2 runtime is installed on your system.

3)If you're using WinForms or WPF, add WebView2 via NuGet. Search and install Microsoft.Web.WebView2.

4)In your application, create and add the WebView2 control to your form or window.

Set the Source property of WebView2 to your ASP.NET Core website's URL. Example: webView2Control.Source = new Uri("http://localhost:5000");

5)Run your application

0
On

You can read HTML file , and then NavigateToString

if (webView!= null && webView.CoreWebView2 != null) { string text = system.IO.File.ReadAllText(@"C:/Users/xxx/Desktop/VisualSelf/self.html"); webView.CoreWebView2.NavigateToString(text); }

or you can Navigate to local file:

webView.CoreWebView2.Navigate("file:///C:/Users/xxx/Desktop/VisualSelf/self.html");