HandleAsServerSideRequest - Can multiple things modify this without conflicting?

If you want to add some custom route handling to Umbraco one of the techniques is to set a value against HandleAsServerSideRequest

This is normally done via something like this:

builder.Services.Configure<UmbracoRequestOptions>(options =>
{
    options.HandleAsServerSideRequest = httpRequest =>
    {
        return httpRequest.Path.StartsWithSegments("/sitemap.xml");
    };
});

My question is, if you have a package that is already setting this for Route A, how can you do the same for something custom in your own project? Each time you set it does it replace existing settings or is there a way (like with events) where you can daisy chain them?

So, after a bit of a discussion a solution to this has been found.

This is the approach that has worked for me, as it introduces a “chain” style implementation:

builder.Services.Configure<UmbracoRequestOptions>(options =>
{
    var next = options.HandleAsServerSideRequest ; // Store the current function
    options.HandleAsServerSideRequest = httpRequest =>
    {
        //If our check returns true return, else call the chain.
        return httpRequest.Path.StartsWithSegments("/sitemap.xml") || next(httpRequest);
    };
});

I would advise anyone implementing this approach consider using this chaining otherwise it becomes a last come takes priority situation and can break packages/dependencies.

Also, Umbraco’s intellisense is incorrect the default return value is false not true :slight_smile: