基于C#的Kiosk自助服务终端外部链接拦截方案咨询
Absolutely, this is totally achievable with C#—let’s walk through exactly how to set up navigation interception for your kiosk app, while only triggering the "Out Of Order" state when a new tab/window is requested.
核心思路
Since you’re building a kiosk app, you’re almost certainly using a web-embedded control like WebView2 (the modern, recommended choice for .NET desktop apps) to render your UI. We’ll leverage two key events from WebView2:
NavigationStarting: To block all unwanted navigation attempts from hidden links in the current tab.NewWindowRequested: To detect when a user tries to open a new tab/window, trigger the "Out Of Order" state, and block the action.
实现步骤(WinForms示例)
First, make sure you’ve added the WebView2 control to your form via NuGet (Microsoft.Web.WebView2). Then follow these steps:
1. Initialize WebView2 and Subscribe to Events
private async void KioskForm_Load(object sender, EventArgs e) { // Wait for WebView2 to initialize its core environment await webViewKiosk.EnsureCoreWebView2Async(null); // Subscribe to navigation events webViewKiosk.CoreWebView2.NavigationStarting += OnNavigationStarting; webViewKiosk.CoreWebView2.NewWindowRequested += OnNewWindowRequested; }
2. Block Unwanted Navigation in Current Tab
This event fires whenever the current tab tries to navigate to a new URL. We’ll cancel all navigation (adjust the logic if you need to allow internal app pages):
private void OnNavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e) { // Cancel all navigation attempts (modify this if you need to whitelist internal URLs) // Example whitelist logic: if (!e.Uri.StartsWith("https://your-kiosk-internal-url.com/")) e.Cancel = true; }
3. Detect New Tab/Window Requests and Trigger "Out Of Order"
When a user tries to open a link in a new tab (right-click > Open in new tab, or a link with target="_blank"), this event fires. We’ll block the new window and activate your failure state:
private void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e) { // Block the default new window behavior e.Cancel = true; // Trigger your "Out Of Order" business logic SetKioskOutOfOrder(); } private void SetKioskOutOfOrder() { // Customize this to match your kiosk's failure flow: // 1. Display an error page webViewKiosk.CoreWebView2.NavigateToString(@" <html> <body style='display:flex; justify-content:center; align-items:center; height:100vh; margin:0;'> <div style='text-align:center;'> <h1 style='color:#dc3545;'>Out Of Order</h1> <p>This kiosk is temporarily unavailable. Please contact support.</p> </div> </body> </html>"); // 2. Disable further user interaction webViewKiosk.CoreWebView2.IsScriptEnabled = false; // 3. Notify admins, log the event, etc. LogKioskFailure("User attempted to open a new tab/window"); } private void LogKioskFailure(string message) { // Implement your logging logic here File.AppendAllText("kiosk_logs.txt", $"{DateTime.Now}: {message}\n"); }
额外注意事项
- WPF Adaptation: If you’re using WPF instead of WinForms, the logic is identical—just subscribe to the same events on the WPF WebView2 control.
- Whitelisting Internal URLs: If your app has legitimate internal navigation, update the
OnNavigationStartingmethod to only block external or unwanted URLs (usee.Urito check the target address). - Legacy WebBrowser Control: If you’re stuck using the old
WebBrowsercontrol, you can use theNavigatingevent to block navigation, but WebView2 is far more reliable for modern web content.
This approach lets you keep your hidden links in the DOM as required, prevents any accidental or intentional navigation from them, and only triggers the "Out Of Order" state when a new tab/window is requested—perfect for your kiosk use case.
内容的提问来源于stack exchange,提问作者George Harrison

