Andrew Simpson
Andrew Simpson

Reputation: 7344

Multiple Endpoints in same controller with different parameter

Been years since I had to do this and the heat must be getting to me!

I have my home controller:

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

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

I have 1 Index.cshtml page.

In my startup,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}");
        });

The error I get is:

**{"error":"APP: The request matched multiple endpoints.

Upvotes: 1

Views: 4840

Answers (2)

Rajdeep D
Rajdeep D

Reputation: 3920

I tried this,

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";
        }
    }
}



Upvotes: 0

Roar S.
Roar S.

Reputation: 11319

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();
    }
}

and in Startup#Configure

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

Upvotes: 1

Related Questions