Pahin
Pahin

Reputation: 1

.NET Core: dynamic routing conflicts with physical files: The request matched multiple endpoints

I have an ASP.NET Core razor pages project.

The code shown here is a gross oversimplification of my general idea, just to show the problem I encountered.

I have a simple physical page /Users/Index.cshtml:

@page
@model RazorTest.Pages.Users.IndexModel
@{
    string id = Request.Query["id"];
}

<h1>Users!</h1>

<h2>@id</h2>

I want to make a dynamic routing for requests to it, so that requests like /Users/{id} go to this page with the parameter id={id}.

I use MapDynamicPageRoute with a DynamicRouteValueTransformer for this.

My program.cs looks like this:

private static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddRazorPages();
    builder.Services.AddScoped<MyRouteTransformer>();

    var app = builder.Build();
    app.UseRouting();
    app.MapRazorPages();
    app.MapDynamicPageRoute<MyRouteTransformer>("{**id}");
    app.Run();
}

My DynamicRouteValueTransformer's simple implementation:

public class MyRouteTransformer: DynamicRouteValueTransformer 
{
    public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
    {
        return await Task.Run(() =>
        {
            string id = values["id"] as string;
            return new RouteValueDictionary()
            {
               { "page", "/Users/Index" },
               { "id", id }
            };
        });
    }
}

However, when accessing the page with the parameter I get an error:

The request matched multiple endpoints /Users/Index.

How can it be resolved?

Upvotes: 0

Views: 59

Answers (0)

Related Questions