I'm trying to create a method which creates a url based on the controllername and the actionname. I don't want to use magic strings, so I was thinking about a method taking a lambda expression as a parameter.
The tricky part is, I don't want to specify any parameters on the action method. So for instance if I have this controller:
public class HomeController : IController
{
public Index(int Id)
{
..
}
}
I would like to call the method like this:
CreateUrl<HomeController>(x=>x.Index);
The signature of the method I've come up with is:
public string CreateUrl<TController>(Expression<Action<TController>> action) where TController : IController
But this does not solve the problem of skipping the parameters. My method can only be called with the parameter specfied like this:
CreateUrl<HomeController>(x=>x.Index(1));
Is it possible to specify an action or method on a controller without having to set the parameters?
Expression<Func<TController, Func<ActionResult>>>
and you'll be able to omit parenthesisc => c.Index
.