Reputation: 2959
I work asp.net MVC, I call an action (let's call it MyAction) from MyController while I'm at the following url www.mywebapp.com/MyController/AnotherAction/123
I need to get the 123 in MyAction, so I started to do something like this :
var url = Request.Url;
In order to parse it then get the ID.
But I'm not sure if it's the "best" way to do it. Do you know how can I get the ID from the current URL when I am in MyAction properly and safely ?
Upvotes: 2
Views: 29626
Reputation: 21
you can get the id from URI Like This:
Cotroller:
public ActionResult Index(int id)
{
//if you want use in view you can save id in view bag
ViewBag.ID = id;
Your Code......
return View(...);
}
and in view use of view bag
View:
@{
ViewBag.Title = "Index";
var ID = ViewBag.ID;
}
Now you have an ID in the variable
Upvotes: 0
Reputation: 14618
You can get this from the RouteData
:
var url = Url.RequestContext.RouteData.Values["id"];
Upvotes: 9