Dynamic RouteTable doesn't work anymore since May 2022

Viewed 87

I have an ASP.NET 4.8 project, Web Form, where I'm using the custom routing. Everything worked perfectly until two months ago when it started not to work correctly.

In the Global.asax I register the routes:


void Application_Start(object sender, EventArgs e)
{
    System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    ...
}

The first line register the routes for the WebAPI I've integrated in the project:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "webapi/{controller}/{id}",
            defaults: new { id = System.Web.Http.RouteParameter.Optional }
        );
}

The second line, register the routes for the resources stored in the database and add also friendlyURLs:


foreach (var r in resources) //resource is coming from the DB...
{
    RouteTable.Routes.Add(new Route(r.ResourceLink.Trim(), new PageRouteHandler("~/ResourceView.aspx")));
}

...

var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);

The routes are working perfectly when I run the application at the beginning. The issue comes out when I add a new resource and I try to update at runtime the routing table. I see from debug that the new route item is added correctly but it's ignored when I made the request.

The code I use to add the resource at runtime is like the one I use to add resource routes from the DB:

RouteTable.Routes.Add(new Route(r.ResourceLink.Trim(), new PageRouteHandler("~/ResourceView.aspx")));

The only difference I see in the RouteTable created at the application start and when I modified it at runtime is that the new route item is added after the routes for the friendlyURLs, but it has worked until a couple of months ago.

Any clue on how to solve this issue?

UPDATE

Inspecting the route table right after I've added a new route at runtime I see it correctly in the Routes collection, identical to the others, loaded from the DB: enter image description here If I try to access the resource at /data-management/data-storage/sample4 I get redirected to the wrong handler which is the last in the list before I added the new one at runtime, even if it doesn't match at all with the Url.

0 Answers
Related