Try our conversational search powered by Generative AI!

CMS 12 Endpoint Areas

Vote:
 

Hello!

It is possible to migrate old CMS 11 project with MVC Areas routing (path to the controller, models and views like "Project_root/Areas/{area}/{subarea}/") to CMS 12 endpoint routing?

Folder structure should be saved.

I'm trying to add routing areas before (also i tried place it after) endpoints.MapContent() method, but when AspNetCore.Mvc Controllers routing works, episerver cms routing not works. Views engine not works too.

Thank you!

#277325
Mar 28, 2022 11:02
Vote:
 

I was running into the same issue where I couldn't get content routing to work with areas. I tried both endpoints.MapControllerRoute() and endpoints.MapAreaControllerRoute(), as shown here, but no matter where I put them (before or after endpoints.MapContent()) or what order I put them in, it didn't work.

I was finally able to get it working by using [Route] attributes instead.

So, in Startup.cs, it looks like this (nothing area-related):

app.UseEndpoints(endpoints =>
{
    // Map Episerver content routes.
    endpoints.MapContent();

    /* Some other unrelated routes here. */

    // Default/fallback route if none of the above matched: {controller=Home}/{action=Index}/{id?}
    endpoints.MapDefaultControllerRoute();
});

And on the individual controllers within the area, it looks like this (see [Area] and [Route] attributes):

[Area("MyAreaName")]
[Route("MyAreaName/[controller]")]
public class MyPageTypeController : PageController<MyPageType>
{
    [HttpGet]
    public ActionResult Index(MyPageType currentPage)
    {
        /* Logic to get model and return view. */
    }
}

This does require putting the same [Route] attribute on all controllers within the area, but you may be able to make a base controller for the area and add the attribute there instead.

#278377
Apr 12, 2022 18:26
Vote:
 

@Alan... I haven't gotten to test it out yet, but I'm curious if just setting the "area" RouteData.DataToken in an ActionFilter would work.

See my post here: https://www.cjsharp.com/blog/2021/01/04/multi-site-episerver-solutions-using-mvc-areas/

Maybe doing it that way would mean you wouldn't need to decorate each controller with the [Area] and [Route] attribute.

#278379
Apr 12, 2022 20:36
Vote:
 

Hey @Chris! It does seem to find the area controllers correctly when you remove the [Area] and [Route] attributes and just set the "area" data token, like you suggested.

One caveat I noticed, though, is that without the [Area] attribute, it doesn't automatically find the correct view when returning View("Index", model) from a controller in the area. I think you'd need to add some custom view location formats (or view expander) via the AddRazorOptions() extension in Startup.cs in order to get it to search the Views folder within the area. When you add the [Area] attribute, it does that automatically.

#278419
Apr 13, 2022 4:17
Vote:
 

Ya. In CMS 11, I had to do some fancy work for blocks, and you can see that in this post: https://www.cjsharp.com/blog/2021/01/15/multi-site-episerver-solutions-using-mvc-areas-block-controllers/ But for pages, I didn't need to do anything special. I'll have to play around with this in CMS 12.

#278452
Apr 13, 2022 19:46
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.