I would like to create custom bullets for pages in my CMS so that users can create their own SEO URLs (e.g. Wordpress).
I used this in Ruby on Rails and PHP frameworks, βabusingβ route 404. This route is called when the requested controller cannot be found, which allows me to redirect the user to my dynamic page controller to parse the bullet (from where I redirected them to the real 404 if the page was not found). Thus, the database was requested only to validate the requested pool.
However, in MVC, the catch-all route is only called when the route does not conform to the default standard /{controller}/{action}/{id}
.
To still be able to parse custom bullets, I modified the RouteConfig.cs
file:
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); RegisterCustomRoutes(routes); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { Controller = "Pages", Action = "Index", id = UrlParameter.Optional } ); } public static void RegisterCustomRoutes(RouteCollection routes) { CMSContext db = new CMSContext(); List<Page> pages = db.Pages.ToList(); foreach (Page p in pages) { routes.MapRoute( name: p.Title, url: p.Slug, defaults: new { Controller = "Pages", Action = "Show", id = p.ID } ); } db.Dispose(); } }
This solves my problem, but requires the Pages
table to be fully requested for each query. Since the overloaded show method ( public ViewResult Show(Page p)
) did not work, I also need to get the page a second time, because I can only pass the page identifier.
- Is there a better way to solve my problem?
- Is it possible to pass the page object to my Show method instead of the page id?
source share