0

I need to build a CMS like website.

I have a Backend (admin website) where users can create or update pages for the Frontend (it will be 2 differents websites but both are .Net Core 6 MVC)

I've already done that with Asp.Net MVC by directly manipulating the RouteCollection of the Front. The Backend was calling an action on the Front that was creating/updating the needed route for the purpose of url rewriting)

Here how I define Route url of all Pages in Front :

        var pages = _pageService.GetAll();
        foreach (CmsPage page in pages)
        {
            routeCollection.MapRoute(
                    name: $"page-{page.PageId}",
                    url: page.url,
                    defaults: new
                    {
                        controller = "PageController",
                        action = "Detail",
                        id = page.PageId,
                    }
                );
        }

This way, in Razor, I can easily list all page's link like this :

@foreach (CmsPage page in Pages)
{
    <a href="@Url.RouteUrl("page-" + page.PageId)">@page.Title</a>
}

If Users in Backend want to change the slug of a Page, the Backend call the Front so he can change the needed entry in the RouteCollection and update the slug :

 protected void UpdateRouteFromRouteTable(string routeName, string newUrl)
 {
        // Get the route by name
        var route = RouteTable.Routes[routeName];

        using (RouteTable.Routes.GetWriteLock())
        {
            ((Route)(route)).Url = newUrl;
        }
 }

By doing this the slug of the page is updated on the Front.

THIS is no more possible in .Net Core (can't access routeTable) but I need to achieve the same goal maybe with some kind of dynamics routing.

Where should I look ? I read about DynamicRouteValueTransformer but it doesn't seems to be what I'm looking for, neither seems to be Microsoft.AspNetCore.Rewrite.RewriteContext...

Any hints ?

Thx a lot

El MoZo
  • 157
  • 1
  • 10
  • 1
    Route in asp.net Core is different from ASP.Net; In ASP.net, the routes registed in RegistRoutes Method which is called in Application_Start method in global.asax.cs,it would only be excuted when the application start. However,in asp .net core the Route is registed in Middleware( would be invoked per request),So I think you may try with url rewrite or try to regist a dynamic route https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-6.0#create-a-middleware-pipeline-with-webapplication – Ruikai Feng Sep 27 '22 at 09:59
  • Thanks @ruika, dynamic route is defintly what I am looking for, and I'm going to try something like this : https://stackoverflow.com/a/11496379/1143650 – El MoZo Sep 27 '22 at 11:02

0 Answers0