Search code examples
c#.netmodel-view-controllerroutesrazor-pages

Razorpage defined route not inserted into the route table on publish


I have a .Net Core 6 project primarily using Razorpages but with some Controllers.

A couple of my routes work fine locally, but disappear when I publish the project.

The routes are,

  • Organisation/Events/Event/63f400800c1ca5bae19369b3
  • Organisation/Events/Event/63f400800c1ca5bae19369b3/Poster

The routes are defined in my Razorpages using the page directive

@page "/Organisation/Events/Event/{eventId}"
@page "/Organisation/Events/Event/{eventId}/Poster"

And I've added a route debugger to my project which spits out this route when I run it locally.

{
"Action": null,
"Controller": null,
"Page": "/Organisation/Events/Event",
"Name": null,
"Template": "Organisation/Events/Event/{eventId}",
"Contraint": []
},
{
"Action": null,
"Controller": null,
"Page": "/Organisation/Events/EventPoster",
"Name": null,
"Template": "Organisation/Events/Event/{eventId:required}/Poster",
"Contraint": []
}

However, when I publish it these routes are nowhere to be found.

I believe I have my startup config configured correctly


builder.Services.AddRazorPages(options =>
{
    options.Conventions.AuthorizeFolder("/Organisation", "RequireOrganisation");
    options.Conventions.AuthorizePage("/OrganisationSelector");

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

Just to note, other routes which use this segment work, so I can access the following path just fine and this is exclicitly configured in the same way.

Organisation/Events/Event/63f400800c1ca5bae19369b3/Tournament/64498dbe517a1fecb8eae004

I feel I'm missing something obvious here but does anything jump out to anyone?

Thanks for reading.

I've tried tweaking my configuration in various ways. But these inconsistencies when published are puzzling.


Solution

  • I got to the bottom of this issue accidentally in the end.

    I deleted one of the pages with the route to see if it was conflicting in some way. I still had the same problem so afterwards I re-added it.

    After re-adding the page it popped up in the route table of the published version. So I deleted the other, re-added and the problem was solved for both.

    Looking at the csproj I can see those files were designated with

    <itemGroup> 
            <Content Remove="Pages\Organisation\Events\Event.cshtml" />
            <Content Remove="Pages\Organisation\Events\EventPoster.cshtml" />
    </itemGroup>
    
    <itemGroup>
        <Script Remove="Pages\Organisation\Events\Event.cshtml" />
          <Script Remove="Pages\Organisation\Events\Event.cshtml.cs" />
          <Script Remove="Pages\Organisation\Events\EventPoster.cshtml" />
          <Script Remove="Pages\Organisation\Events\EventPoster.cshtml.cs" />
    </itemGroup>
    
    <itemGroup>
    <None Include="Pages\Organisation\Events\Event.cshtml" />
          <None Include="Pages\Organisation\Events\EventPoster.cshtml" />
    </itemGroup>
    

    I've no idea when this happened, and it's not something I would have purposely set against these pages. But it's interesting that it worked when running locally but not when published.