user2803474
user2803474

Reputation: 21

How to get url id value in controller?

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);
        }

}

Upvotes: 2

Views: 2991

Answers (1)

Amit
Amit

Reputation: 15387

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)

Upvotes: 0

Related Questions