Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel navigation does not work when clicking link with malformed Uri #400

Closed
cbra-caa opened this issue Aug 20, 2020 · 15 comments
Closed
Labels
bug Something isn't working

Comments

@cbra-caa
Copy link

cbra-caa commented Aug 20, 2020

Setup

  • WPF application
  • .Net 4.7.2
  • 0.9.579-prerelease package for WebView
  • Edge Dev installed

Problem

The user sees a links and clicks it. Sadly the link leads to nowhere due to, for example, a space placed in the middle of the link. At the moment I catch this errors and then cancel the navigation event but the navigation still completes and shows the dead page to the user.

Code

I have created a minimum viable example which can reproduce the issue. Create a new WPF app with the following code:
MainWindow.xaml

<Window x:Class="TestWebView2App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
  
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Button x:Name="LinkButton" 
            Content="Get me a bad link"
            Click="Button_Click" />

    <wv2:WebView2 Name="WebView"
                  Grid.Row="1"
                  Source="http://microsoft.com" />

  </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Windows;

namespace TestWebView2App
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      WebView.NavigationStarting += WebView_NavigationStarting;
    }

    private void WebView_NavigationStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs e)
    {
      if (Uri.IsWellFormedUriString(e.Uri, UriKind.Absolute) == false)
      {
        MessageBox.Show("Navigation cancelled!");
        e.Cancel = true;
      }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      //Navigate to bad link
      WebView.CoreWebView2.Navigate("https://www.micro soft.com");
    }
  }
}

How to reproduce

  1. Open the app
  2. Press the button "Get me a bad link"
  3. See that the navigation event completes (Not expected)
  4. See that the navigation event fires twice (Not expected)

Cancel works as expected for our scenarios.

AB#28631879

@champnic champnic added the bug Something isn't working label Aug 21, 2020
@champnic
Copy link
Member

Thanks for reporting this! I've opened a bug on our side to investigate.

@cbra-caa
Copy link
Author

Awesome, quick question @champnic , I recently updated the description of an older issue I create. Do you check back for those or should I rather have closed the old issue and created it anew?

@champnic
Copy link
Member

I'm currently doing a pass of all older issues to verify we are still tracking everything and closing out old issues but it's not going super fast :P If it's a different bug please open a new issue. Otherwise, can you link the issue and I can take a look?

@cbra-caa
Copy link
Author

I can see that you found it, awesome. For good measure I also added an example to #398 ;)

Thanks for having quick feedback !

@maurawinstanley
Copy link
Contributor

Hi @cbra-caa I am trying to repro your bug and when clicking the button to navigate to the malformed site, the navigation does complete and I am wondering if you are still able to repro the bug? I am using the same nuget package and .net release.

@cbra-caa
Copy link
Author

Hi @maurawinstanley, I can still reproduce the problem, even in the newest 1.0.664.37 stable version.

The flow is that when you navigate to the bad link (there is a space in the middle of the address) the URI validation finds the problem and cancels the request. But the problem is that the navigation completes, as the check WebView_NavigationStarting in has cancelled the request, thus I was expecting to stay on the original page, not navigate to the broken page.

Start page, where i expect to stay
image

Bad page, where i actually end up (even though the event for it was cancelled).
image

@fsuk
Copy link

fsuk commented Dec 1, 2020

I too am experiencing this bug. Even the following dosn't work:

private void WebView1_NavigationStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs e)
        {
            try
            {
                e.Cancel = true;

Using Microsoft.Web.WebView2 1.0.674 with WebView2 Runtime 87.0.664.52 on Windows 7

@fsuk
Copy link

fsuk commented Dec 2, 2020

A partial work around is to call WebView2.Stop()
HOWEVER this also stops all other navigation and loading of resources with the potential to cause problems on the current page if the navigation event was called before they had finished loading.

@Symbai
Copy link

Symbai commented Dec 2, 2020

@fsuk: Your issue should be solved by upgrading to latest prerelease version which fixes your specific issue. But that might be different to the issue this topic is about.

@fsuk
Copy link

fsuk commented Dec 2, 2020

@Symbai i am already using the latest (non-deprecated) prerelease nuget for WPF (1.0.674)

@Symbai
Copy link

Symbai commented Dec 2, 2020

I said prerelease version and the latest is 1.0.707-prerelease which is newer than yours 1.0.674. //edit: See its deprecated now because its buggy. Well it still fixes your problem. Either take it or wait until next version.

@fsuk
Copy link

fsuk commented Dec 2, 2020

1.0.707-prerelease has been marked as deprecated.
I did try this version but got other errors initialising, however i will give it another try.

@champnic
Copy link
Member

champnic commented Dec 4, 2020

@fsuk You're likely running into #678 (which is why we deprecated that package) and you can workaround by using Edge Canary/Dev.

@maurawinstanley
Copy link
Contributor

@fsuk @cbra-caa Thanks for letting us know about the bug, the fix will be in the next release!

@champnic
Copy link
Member

This is fixed in 1.0.781-prerelease SDK package, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants