Creating MapControllerRoute from within the contro...
# help-with-umbraco
s
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.
s
Of course, and it works just like it should. Thanks for pointing me in the right direction 💪
j
You can set up generic controller routing where you register all routes in startup and then you can just add the [Route] attribute to any and all controllers to have them mapped. https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-8.0#attribute-routing-for-rest-apis It can be set within the startup file right before the umbraco controller routes are set like in the attached image 🙂 https://cdn.discordapp.com/attachments/1227230801456795729/1227520900098953297/image.png?ex=6628b4ec&is=66163fec&hm=4157493a1b988ff3df84d95f73647d08b5efd045b77e7f81f973dd561d3d9a3a&
m
@Jemayn My preference for within a composer aligns with @Sebastian Dammark 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.
j
@Mike Chambers 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):
Copy code
csharp
public void Compose(IUmbracoBuilder builder)
{
    builder.Services.Configure<UmbracoPipelineOptions>(options =>
    {
        options.AddFilter(new UmbracoPipelineFilter("RegisterCustomRoutes")
        {
            Endpoints = app => app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            })
        });
    });
}
m
@Jemayn just had a look in a v11.5 project of mine.. and I'm using
Copy code
csharp
    [Route("api/plotinfo")]
    public class PlotInfoController : Controller
    {...}
without the explicit
endpoints.MapControllers
🤔 looks like Umbraco gives it to us out of the box https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Cms.Api.Management/DependencyInjection/ApplicationBuilderExtensions.cs#L65
j
Oh cool, looks like it was added along with the management API, good to know - definitely needed to add it early on, but looks like I can clean it up a bit more now 🙂 🎉
29 Views