C# If URL path does not exist. Redirect to Notfound.aspx page

Use the following code:

if (!System.IO.File.Exists(Server.MapPath(Request.Url.AbsolutePath))) { Response.Redirect("~/Notfound.aspx"); }

Related articles:

Handling Invalid URLs in C#: How to Redirect to a Custom Notfound Page
The web is an ever-evolving space, and it's common to encounter invalid URLs. When users try to access such URLs, they are greeted with an HTTP 404 error. The default 404 error page doesn't provide the best user experience. Hence, it's essential to handle invalid URLs and redirect them to a custom notfound page.

C# Tips: Redirecting Users to a NotFound Page for Invalid URL Paths
Sometimes, when users enter a URL that does not exist on your website, they may encounter a "404" error, indicating that the requested page could not be found. This can be frustrating for users, who may give up on your website and leave if they repeatedly encounter this error.

Creating a Custom 404 Page in C#: Redirecting Users to Notfound.aspx
A custom 404 page is a great way to improve the user experience on your website by providing users with easy-to-understand error messages when they encounter a broken link or mistype a URL. A custom 404 page can also help you to retain visitors, reduce bounce rates, and ultimately increase conversions.