Ok, maybe the title isn't the most descriptive thing in the world, but this problem is killing me. What I'm trying to do is create an ActionLinkFor helper method, like so:
public ActionResult Index()
{
// Does Stuff
}
public ActionResult SomeAction(int param1)
{
// Does Stuff
}
These are two action methods. Action methods can have any number of parameters, but for the purposes of my expression, I don't care. Ideally, what I have been trying for is something like this:
<%= Html.ActionLinkFor<HomeController>(m => m.Index, "Return Home") %>
which would return
<a href="/Home/Index">Return Home</a>
based on the expression it parsed. The objective being that now I'm not using 'magic strings' for determining the links, but instead a compiled lambda expression. If anything was refactored, or a page was changed location, it would be clear what links had been pointing where.
The problem is, the lambda expression for that method handle doesn't seem to resolve to the right overload. I figured I would have to make a few overloads like so:
public static MvcHtmlString ActionLinkFor<Controller>(this HtmlHelper helper, Expression<Func<Controller, ActionResult>> methodExpression, string linkName)
{
// return parsed expression translated into a link
}
public static MvcHtmlString ActionLinkFor<Controller>(this HtmlHelper helper, Expression<Func<Controller, object, ActionResult>> methodExpression, string linkName)
{
// Does the same thing
}
That way, when I said
m => m.Index // Chooses first ActionLinkFor since the Lambda would be a Func<Controller, object>
m => m.SomeAction // Chooses second ActionLinkFor since the method signature is different.
However, this doesn't seem to be working. If I specify multiple generic parameters like so:
<%= Html.ActionLinkFor<HomeController, int>(m => m.SomeAction, "Some Link") %>
it picks it up just fine, but I don't want to have to specify the method signature in the generic argument list. So my questions are:
1) Is there a way to 'target' a method name regardless of it's signature? I can use reflection to get all the methods and use a magic string, but I want there to be a lambda expression at design time so I can specify the link then.
2) If you can provide a solution, is there a simple way of parsing out the method name? The algorithm I'm currently using is more than likely a bad idea, and effectively involves a few conversions and then eventually parsing a string when I couldn't get it to a more convenient type.