netchx/netch · error · MessageException
GetBestRoute 搜索失败
Error message
GetBestRoute 搜索失败
What it means
Calls the Win32 IP Helper GetBestRoute with destination 114.114.114.114 and source 0.0.0.0 to discover the default next-hop gateway and outbound interface index, then builds a NetRoute template used by redirector/TUN routing. GetBestRoute returns a non-zero error code when no matching IPv4 route exists in the forwarding table, so this fires whenever the host has no usable IPv4 route to that destination. The thrown MessageException is not caught locally; it propagates to callers building route templates.
Source
Thrown at Netch/Models/NetRoute.cs:21
namespace Netch.Models;
public struct NetRoute
{
public static NetRoute TemplateBuilder(string gateway, int interfaceIndex, int metric = 0)
{
return new()
{
Gateway = gateway,
InterfaceIndex = interfaceIndex,
Metric = metric
};
}
public static NetRoute GetBestRouteTemplate()
{
if (PInvoke.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route) != 0)
throw new MessageException("GetBestRoute 搜索失败");
var gateway = new IPAddress(route.dwForwardNextHop);
return TemplateBuilder(gateway.ToString(), (int)route.dwForwardIfIndex);
}
public int InterfaceIndex;
public string Gateway;
public string Network;
public byte Cidr;
public int Metric;
public NetRoute FillTemplate(string network, byte cidr, int? metric = null)
{
var o = (NetRoute)MemberwiseClone();View on GitHub (pinned to 9d99eb1c5a)
Solutions
- Enable a network interface and ensure an IPv4 default gateway is configured (verify with `route print` or `ipconfig`).
- Reconnect to Wi-Fi/Ethernet and retry - the template is rebuilt each time routing is set up.
- If a VPN/TUN client removed the default route, disconnect it or restore the route before starting Netch.
- As a last resort add a persistent default route: `route add 0.0.0.0 mask 0.0.0.0 <gateway>` (run as admin).
Example fix
// before
if (PInvoke.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route) != 0)
throw new MessageException("GetBestRoute 搜索失败");
// after - capture the error code for diagnostics
var rc = PInvoke.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route);
if (rc != 0)
throw new MessageException($"GetBestRoute failed (error={rc}). Ensure an IPv4 default route exists."); Defensive patterns
Strategy: validation
Validate before calling
// Verify an IPv4 default route exists before building the route template
var hasRoute = NetworkInterface.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up)
.Any(n => n.GetIPProperties().GatewayAddresses.Any(g => g.Address.AddressFamily == AddressFamily.InterNetwork));
if (!hasRoute)
throw new MessageException("No IPv4 default route; connect to a network first."); Type guard
bool HasIpv4DefaultRoute() => NetworkInterface.GetAllNetworkInterfaces()
.Any(n => n.OperationalStatus == OperationalStatus.Up
&& n.GetIPProperties().GatewayAddresses.Any(g => g.Address.AddressFamily == AddressFamily.InterNetwork)); Try / catch
try { var template = NetRoute.GetBestRouteTemplate(); }
catch (MessageException ex) when (ex.Message.Contains("GetBestRoute"))
{
Global.MainForm.NotifyTip("No network route found. Connect to a network and retry.");
return;
} Prevention
- Gate GetBestRouteTemplate behind a connectivity check (e.g. NetworkInterface.GetIsNetworkAvailable()).
- Defer route-template building until NetworkChange.NetworkAddressChanged reports an Up IPv4 interface.
- Never call it during startup before network initialisation completes.
When it happens
Trigger: NetRoute.GetBestRouteTemplate is invoked while there is no IPv4 default route: all interfaces down/disabled, no default gateway configured, airplane mode, or a VPN/TUN client that deleted the default route. Any non-zero return from PInvoke.GetBestRoute triggers it.
Common situations: Starting Netch with no network; Wi-Fi/Ethernet disabled; a misconfigured VPN that removed the default route; a freshly imaged machine with no gateway; a custom routing policy that blackholes 114.114.114.114.
Related errors
- GetBestRoute 搜索失败
- Lookup Server hostname failed
- The {0} port is in use.
- The {0} port is reserved by system.
- The {0} port is used by {1}.
AI-assisted analysis of netchx/netch@9d99eb1c5a (2026-08-13).
Data as JSON: /api/errors/3d6c79e5e5cdf9e7.
Report an issue: GitHub.