MapMvcAttributeRoutes using optional id

I am using MapMvcAttributeRoutes and would like to have an optional id, but I don't know the proper syntax. Help would be appreciated

I am trying something as:

 // GET: Statistikk/Meg
[Route("Statistikk/Meg/{id:regex(UrlParameter.Optional)}")]
public ActionResult MegIndex()
{
    return View("Meg/Index");
}

Custom Route I am replicating

        routes.MapRoute(
            "StatistikkMeg",
            "Statistikk/Meg/{id}",
            new { controller = "Statistikk", action = "MegIndex", id = UrlParameter.Optional }
        );
728x90

1 Answers MapMvcAttributeRoutes using optional id

This can be done with making the id nullable

    [Route("Statistikk/Meg/{id?}")]

More info at https://devblogs.microsoft.com/aspnet/attribute-routing-in-asp-net-mvc-5/

6 days ago