具有不同参数的同一控制器中的多个端点

Multiple Endpoints in same controller with different parameter

自从我不得不这样做以来已经有好几年了,一定是热到我身上了!

我有我的家庭控制器:

    public ActionResult Index(string param1, string param2, string param3)
    {
        return View();
    }

    public IActionResult Index()
    {
        return View();
    }

我有 1 Index.cshtml 页。

在我的启动中,cs:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}");

            endpoints.MapControllerRoute(
                name: "default2",
                pattern: "{controller=Home}/{action=Index}/{param1}/{param2}/{param3}");
        });

我得到的错误是:

**{"error":"APP: 请求匹配多个端点。

public class HomeController : Controller
{
    // hits when navigating to https://localhost:5001/one/two/three
    [HttpGet("{param1}/{param2}/{param3}")]
    public IActionResult Index(string param1, string param2, string param3)
    {
        return View();
    }

    // hits when navigating to https://localhost:5001/
    public IActionResult Index()
    {
        return View();
    }
}

并在 Startup#Configure

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

我试过了,

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;

namespace RouteTemplateProvider.Controllers
{
    public class RouteWithParamAttribute : Attribute, IRouteTemplateProvider
    {
        public string Template => "{param1}/{param2}/{param3}";
        public int? Order { get; set; }
        public string Name { get; set; }
    }
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [RouteWithParam]
        public string Index(string param1, string param2, string param3)
        {
            return "Index with param";
        }

        public string Index()
        {
            return "Index no param";
        }
    }
}