How to use Default Values URI Parameters? | ASP.Net MVC InterviewQuestion

, ,


  • To that you can specify adefault value  to the route parameter
  • It's like this : [Route("Pet/Breed/{petKey=123}")]

       PetController.cs
        

    public class PetController : Controller
     {
        // eg: /Pet/Breed
        // eg: /Pet/Breed/528
        [Route("Pet/Breed/{petKey=123}")]
        public ActionResult GetSpecificPet(string petKey)
        {
            return View();
        }
     }


Key point of the above code

    • In the above example, both/Pet/Breedand /Pet/Breed/528 will route to the “GetSpecificPet”Action

    Above Route on Browser is as below