-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);
}
mrjamiebowman
  • 105
  • 14
  • 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? – BradleyDotNET Jun 05 '14 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? – xDaevax Jun 05 '14 at 18:11
  • Avoid allowing code to be passed in. Instead, pass in json and then deserialize it. Related: http://stackoverflow.com/q/2555363/1026459 – Travis J Jun 05 '14 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 – mrjamiebowman Jun 05 '14 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. – xDaevax Jun 05 '14 at 18:18
  • I need to replace the Url helper in MVC. Url.Action(ActionName, ControllerName, new { id = 3}) – mrjamiebowman Jun 05 '14 at 18:19
  • Why do you need to replace the built-in functionality? Does it not work for you for some reason? – xDaevax Jun 05 '14 at 18:26

2 Answers2

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

Community
  • 1
  • 1
xDaevax
  • 2,012
  • 2
  • 25
  • 36
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)

mrjamiebowman
  • 105
  • 14
  • 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 Jun 05 '14 at 18:34
  • xDaevax corrected me. He has the best answer. I was trying to recreate the Url.Action method for a unit test. – mrjamiebowman Jun 05 '14 at 19:33
  • I have updated my answer with some relevant information for that scenario. – xDaevax Jun 05 '14 at 19:42