-4

Simple, I want to parse the objects[] parameters to it's value. It returns "{ id = 3 }" if I pass in this value. I want to do string id = 3... How is this possible?

Reason .NET MVC does it this way: Url.Action(ActionName, ControllerName, new { id = 3})

I want to get the value of an Annonymous Type.

GetUrlStringStringObjectArray = (string actionName, string controllerName, **object[] parameters**) =>
{
    Assert.AreEqual<string>(EventsController.Actions.Register.ToString(), actionName, "Url.Action called with an incorrect action.");
    Assert.AreEqual<string>(EventsController.ControllerName, controllerName, "Url.Action called with an incorrect controller.");

    string id = parameters[0].ToString();
    // returns "{ id = 3}"

    if (!String.IsNullOrWhiteSpace(id)) 
        return String.Format("/{0}/{1}/{2}", controllerName, actionName, id);
    else
        return String.Format("/{0}/{1}", controllerName, actionName);
}
7
  • You might be able to get the dynamic keyword to help with this, but it sounds like you are just trying to have run-time generated variables, which is not a supported concept in C#. Are you trying to do something else? Commented Jun 5, 2014 at 18:06
  • So, you want to strip out the {} brackets or the brackets and the "id = "? Is this JSON data being passed to this method? Commented Jun 5, 2014 at 18:11
  • Avoid allowing code to be passed in. Instead, pass in json and then deserialize it. Related: stackoverflow.com/q/2555363/1026459 Commented Jun 5, 2014 at 18:13
  • Maybe it is json and I'm just being brain dead.. but valid json should be {"id":"3"} right? I want to do it like this b/c this is how the Url.Action helper works in MVC Commented Jun 5, 2014 at 18:15
  • The fact that it is coming in as an object[] smells funny. Why not have this method take a RouteValueDictionary instead? This will separate out the id and the value (and you can combine them together if you want). This is generally how routing works in MVC, with name value pairs for route data. It would help if you could add more context to the code you provided. Commented Jun 5, 2014 at 18:18

2 Answers 2

1

Change your method to take a RouteValueDictionary instead of an array of object.

var url = GetUrlStringStringObjectArray = (string actionName, string controllerName, RouteValueDictionary parameters) => {
    Assert.AreEqual<string>(EventsController.Actions.Register.ToString(), actionName, "Url.Action called with an incorrect action.");
    Assert.AreEqual<string>(EventsController.ControllerName, controllerName, "Url.Action called with an incorrect controller.");

    string id = parameters["id"].ToString();

    if (!String.IsNullOrWhiteSpace(id)) 
        return String.Format("/{0}/{1}/{2}", controllerName, actionName, id);
    else
        return String.Format("/{0}/{1}", controllerName, actionName);
}

Edit: Here are some additional resources on Object Initializer syntax in CSharp

Edit (2): I will leave the other code above but if you're trying to unit test something that uses the UrlHelper extensions, it isn't that easy (though it can be done). I won't re-answer that here, but there are many other questions related to that.

ASP.NET MVC: Unit testing controllers that use UrlHelper

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

1 Comment

This seems more correct. Thanks.
0

My coworker helped me. This is Exactly what I wanted to do. It's an Anonymous type. Using reflection I can get the value of an object.

values.GetType().GetProperties()[0].GetValue(values, null)

3 Comments

That seems like an overly complex (and expensive) solution. If this is production code, there is likely a much more efficient (and readable) way to accomplish what you want without reflection. Can you provide any additional details about the problem you are trying to solve?
xDaevax corrected me. He has the best answer. I was trying to recreate the Url.Action method for a unit test.
I have updated my answer with some relevant information for that scenario.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.