0
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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This code comes from the XUnit examples. Download the latest release to get the full code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.