endpoints.MapControllerRoute Pattern can't using fixed prefix?

Sherry Patter 1 Reputation point
2021-11-22T01:09:54.13+00:00

Why below routing is not working and match?

ep.MapControllerRoute(
       name: "NewRoute",
       pattern: "Blog/Do{action}",
       defaults: new { controller = "Home" });

when accessing

/Blog/DoIndex

Its showing error with AmbiguousMatchException?

Can Someone help?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,199 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Brando Zhang-MSFT 2,961 Reputation points Microsoft Vendor
    2021-11-23T05:59:03.287+00:00

    Hi SherryPatter-2252,

    According to your route, it seems you want to match the home controller's routes. But inside your pattern, it doesn't contain any controller information.

    For this kind of situation, we suggest you could consider using the attribute route. Besides, the attribute route support token replacement feature which could achieve your requirement. Notice this feature is not supported by the Conventional routing(This is the reason why your route will match all the pattern).

    More details, you could refer to below codes and this article:

        [Route("Blog/Do[action]")]  
        public class HomeController : Controller  
        {  
            private readonly ILogger<HomeController> _logger;  
      
            public HomeController(ILogger<HomeController> logger)  
            {  
                _logger = logger;  
            }  
           
            public IActionResult Index()  
            {  
                return View();  
            }  
      
            public IActionResult Privacy()  
            {  
                return View();  
            }  
      
            [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]  
            public IActionResult Error()  
            {  
                return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });  
            }  
        }  
    
    1. List item