Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC routing with optional parameter

I have this route set up:

routes.MapRoute(
    "home3", // Route name
    "home3/{id}", // URL with parameters
    new { 
        controller = "home", 
        action = "Index", 
        id = UrlParameter.Optional } // Parameter defaults
);

But in my controller I don't know how to get the optional id parameter. Can someone explain how I can access this and how I deal with it being present or not present.

Thanks

like image 637
Illy Coslova Avatar asked Nov 29 '22 10:11

Illy Coslova


1 Answers

your can write your actionmethod like

public ActionResult index(int? id)
{
   if(id.HasValue)
   {
       //do something  
   }
   else
   {
      //do something else
   }
}
like image 171
Muhammad Adeel Zahid Avatar answered Dec 11 '22 01:12

Muhammad Adeel Zahid