Accessing URL parameters on MVC framework
public ActionResult Edit(string param)
{
}
开发者_如何学编程http://localhost/Edit/5,someothervalue,etc
How can i reach 'param' from a global function. Basically I am trying to access the parameter in my url from a statinc method that I am building which will be global. That method breaks down the parameter and returns id and other things that I am passing inside param
I think you're looking for a way to get the route values from a given URL. Here's some code that might help you do this with a URL string. Just note that I put a IRouteRegistrant interface that just has a Register function that takes a route collection. Basically replace that with your registration mechanism.
public static RouteData GetRouteValues(IRouteRegistrant registrant, string url)
{
var routes = new RouteCollection();
registrant.Register(routes);
var context = new FakeHttpContext(url);
return routes.GetRouteData(context);
}
So to get at the values (for your param example) you just need the following:
public static void MyFn()
{
var values = GetRouteValues(....., "~/Edit/5");
var paramValue = values.Values["param"];
.....
}
Hope this helps.
精彩评论