2

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

}
user2803474
  • 21
  • 2
  • 9

1 Answers1

0

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)

Amit
  • 15,217
  • 8
  • 46
  • 68
  • i am using the default route.Here is my code routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); – user2803474 Jan 16 '15 at 11:28