Search code examples
razorduende-identity-serverfast-endpoints

Razor Pages should only listenen to specific route


I have a project with an existing FastEndpoints REST API. Nothing special, pretty much the default configuration:

app.UseFastEndpoints(x => {
    x.Endpoints.Configurator = definition => {
        definition.Description(desc => desc.ProducesProblemFE<CommonErrorResponse>(500));
        definition.Description(desc => desc.ProducesProblemFE<ErrorResponse>());
    };
    x.Endpoints.Filter = ep => true;
});

Now I want to add the Duende IdentityServer, which I guess doesn't matter too much, because the real problem are the Razor pages of this package. Again, nothing special:

builder.Services.AddRazorPages();

The problem now is, that the Razor pages take precedence over the REST API. That means, if I want to go to GET users/self I get shown a razor page instead of the correct data or an HTTP error.

So what I'd like to do is one of the following:

  • configure Razor to only listen to a specific route
  • configure FastEndpoints to override Razor's setting for its route

I could not find anything on either of this frameworks (only a solution for MVC), so I'm asking here: how do I do that?


Solution

  • the following middleware order seems to work:

    var bld = WebApplication.CreateBuilder(args);
    bld.Services
       .AddFastEndpoints()
       .AddRazorPages();
    
    var app = bld.Build();
    
    if (!app.Environment.IsDevelopment())
        app.UseExceptionHandler("/Error");
    
    app.UseRouting()
       .UseAuthorization()
       .UseFastEndpoints();
    app.MapStaticAssets();
    app.MapRazorPages()
       .WithStaticAssets();
    app.Run();
    

    test endpoint:

    sealed class TestEndpoint : EndpointWithoutRequest
    {
        public override void Configure()
        {
            Get("test");
            AllowAnonymous();
        }
    
        public override async Task HandleAsync(CancellationToken c)
        {
            await SendAsync("hello...");
        }
    }
    

    making a get request to /test shows the following api response:

    GET http://localhost:5234/test
    
    HTTP/1.1 200 OK
    Content-Type: application/json; charset=utf-8
    Date: Wed, 08 Jan 2025 17:34:24 GMT
    Server: Kestrel
    Transfer-Encoding: chunked
    
    "hello..."
    
    Response code: 200 (OK); Time: 3ms (3 ms); Content length: 10 bytes (10 B)