ASP.NET Core Web API 常规路由

ASP.NET Core Web API Conventional routing

在ASP.NET Core Web API中,我正在使用属性路由,我需要将其移动到常规路由。

 [ApiController]
 public class HomeController : Controller
 {
    [Route("GetHome")]
    [AllowAnonymous]
    [HttpGet]
    public string Getdeatils([FromBody] myclass cs, string system)
    {
    }
 }

使用属性路由 URL localhost/GetHome?system=abc 工作并触发 Getdeatils 方法。

我正在尝试将相同的 URL 模式移动到启动,但我无法实现 it.I 已从控制器中删除 [ApiController] class 我尝试了以下代码在启动。但它不起作用。

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
               name: "mycustom",
               pattern: "GetHome",
               defaults: "{controller=Home}/{action=Getdeatils}/{system?}");
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{system?}");
            
        });

URL:localhost/Home/Getdeatils?system=abc - 这很好

URL:localhost/GetHome?system=abc - 这不起作用。

如何在不更改 URL 格式的情况下实现这一目标。

请尝试使用此代码:

app.UseEndPoints(endpoints =>
{
  endpoints.MapControllerRoute(name: "mycustom",
           pattern: "GetHome",
           defaults: new {controller="Home", action="GetDetails"});
  endpoints.MapControllerRoute(name: "default",
           pattern: "{controller=Home}/{action=Index}/{system?}");
}

如果它按预期工作,请告诉我。