#Creating MapControllerRoute from within the controller ?

1 messages · Page 1 of 1 (latest)

fallow heath
#

I've created 2 controllers for robots and sitemap.
I've also created 2 MapControllerRoute for robots.txt and sitemap.xml in Startup.cs

Everything is dandy 🙂

But being me and knowing my ability to remember, I for sure will zapp my memory regarding MapControllerRoutes on my next assignment.
So here the question goes 🙂

Would it be possible to do it from within the controllers ?
This way it all will located the same place.

fallow heath
#

Of course, and it works just like it should.
Thanks for pointing me in the right direction 💪

peak kiln
bronze wyvern
#

@peak kiln My preference for within a composer aligns with @fallow heath in that it's much more modular to lift and shift to another project, indeed can be kept in a code library project. It has the added benefit that keeping startup.cs clean means you can leave Umbraco in charge of startup.cs.. so for instance when moving to v13 it;s much easier to align to program.cs (as startup.cs isn't needed for the new minimum net core project approach)
As umbraco updates also don't touch startup.cs/program.cs you can get stung, (I remember a breaking namechange in some builders at one point, and the afforementioned program.cs shift too). Indeed, I might be being overkill, but as part of my update process I install a vanilla version of the current version and new version and do a file comparison to check for the hidden changes that the upgrade process might miss. Though I have found this also useful for being able to test non working funtionality in my project against a vanilla umbraco install without having to chase down any slight differences due to startup.cs/program.cs changes.

peak kiln
#

@bronze wyvern
That makes perfect sense!

My addition was more to show that it's not nessecary to explicitly register all routes, can just use the .MapControllers() method in startup and then add route attributes to the controllers, saves having to add new things multiple places and can just add the attributes as the controller is developed 🙂

Same as registering it in the startup file it looks like it could be registered in a composer - something like this (Not tested):

public void Compose(IUmbracoBuilder builder)
{
    builder.Services.Configure<UmbracoPipelineOptions>(options =>
    {
        options.AddFilter(new UmbracoPipelineFilter("RegisterCustomRoutes")
        {
            Endpoints = app => app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            })
        });
    });
}
bronze wyvern