I'm trying to get the URL segments of a page request in the global.asax file using HttpContext.Current.Request.RawUrl. My code is as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new { controller = "Home|Account" }
);
var url = HttpContext.Current.Request.RawUrl;
var pageTitle = url.Split('/')[1];
routes.MapRoute(
"SendToCustomController",
"{*url}",
new { controller = "Custom", action = "Index", title = pageTitle }
);
}
The first time if I type in an address such as devserver:1234/test, I get the string 'test' sent through to the controller. The problem is, in subsequent requests that have a different segment such as devserver:1234/confused, I still get 'test' sent through to the controller.
The aim of this is for any request that doesn't have a designated controller, the controller segment of the URL is passed to a custom page controller which will see if there are any user generated pages in the DB with that title. If there is, the page will be loaded, if there's not the standard 404 will be thrown.
Any help to sort out the issue above, or a better way to achieve this will be great!