最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - .NET Core: dynamic routing conflicts with physical files: The request matched multiple endpoints - Stack Overflow

programmeradmin1浏览0评论

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?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论