。net Core中按名称定义MVC路由

问题描述 投票:0回答:1

我定义了2条路线:

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapControllerRoute("api", "{controller=Home}/api/v1/{action=Index}/{id?}");
        });

在控制器中,如果我未在控制器中指定路由,它将使用其中任何一个。这两个网址均有效:

https://myapp/mymodel/api/v1/id/123

https://myapp/mymodel/id/123

我希望它仅与第一个URL一起使用,但是如果将例如[Route(“ api”)]添加到控制器,则上述路由均无效。

[Route("api")] //with this line it returns 404
public mymodel ID(int? id)
{
  //some code
 }
asp.net-mvc asp.net-core routes
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.