How to get url id value in controller?

2.9k Views Asked by At

I want to get the id of my current url and use it in a controller. http://localhost:14160/?id=69 this Request.RequestContext.RouteData.Values["id"] is returning null value. Any ideas?

controller:

public class HomeController : Controller
    {
        public ActionResult Index(int? id = null)
        {
            ViewBag.ID = id;
            return View();
        }
    }

controller i want to get the id at:

public class FacilityAddController : Controller
    {
 public ActionResult GetID()
        {

           var id= Request.RequestContext.RouteData.Values["id"];

            return Json(id, JsonRequestBehavior .AllowGet);
        }

}
1

There are 1 best solutions below

1
On

There is no controller name, please add controller name and action name with parameter if parameter is different in routing.

For example:

http://localhost:14160/Home/Index?id=69

http://localhost:14160/Home/Index/69(in case id is defined in routing as optional paramter)