Search code examples
asp.netasp.net-mvcasp.net-coreasp.net-mvc-routing

Routing attribute with fixed parameter value in asp.net core


Is there any way to configure attribute routing with fixed parameter in .net core

[Route("TermsOfUse")] // ex: i need push default routing with id = 5
public async Task<IActionResult> Details(int id) { }

I found a way to use routes.MapRoute

routes.MapRoute(null, "TermsOfUse", new { controller = "Article", action = "Details",  id=5 })

How to use attribute routing?


Solution

  • You can do as follow:

    [Route("TermsOfUse/{id=5}")]
    public async Task<IActionResult> Details(int id) { }
    

    If no value for id is supplied then it will be 5.