0

I would like to handle all requests to not existing routes with MyRouteHandler If a request to an /existingfolder/existingpage.aspx happen I would like to serve it normal way by the server. (not only for pages, but scripts, images also). If a request coming to a not existing resource I would like to my MyRouteHandler to catch and serve it by instantiating an appropriate handler.

Currently I am using the following code, which is working, but ugly and not efficient I suppose:

private void RegisterMyRoutes(RouteCollection routes)
{
    var myRouteHandler = new MyRouteHandler();

    routes.Add(new Route("{f1}", MyRouteHandler));
    routes.Add(new Route("{f1}/{f2}", MyRouteHandler));
    routes.Add(new Route("{f1}/{f2}/{f3}", MyRouteHandler));
    routes.Add(new Route("{f1}/{f2}/{f3}/{f4}", MyRouteHandler));
    routes.Add(new Route("{f1}/{f2}/{f3}/{f4}/{f5}", MyRouteHandler));
}

Besides of the ugliness, of course this will not apply to a request with six level path, like /f1/f2/f3/f4/f5/f6.

g.pickardou
  • 32,346
  • 36
  • 123
  • 268

1 Answers1

0

You have two option either you can override exception filter attribute and handle the 404 page or you can change in route config here are two links describing these approach :

ASP.NET MVC 5 - (HTTP Error 404.0 - Not Found) with long non-existing URL

http://tech.trailmax.info/2013/08/error-handling-in-mvc-and-nice-error-pages/

Community
  • 1
  • 1
Varun Vasishtha
  • 461
  • 2
  • 9
  • I do not know it is matter or not, but my site is not ASP MVC. It is simple ASP with mainly WebForms ASP pages. – g.pickardou Oct 26 '15 at 13:43