I am trying to create a method that creates a url based on controllername and actionname. I don't want to use magic strings, so I was thinking of a method that takes a lambda expression as a parameter.
The hard part is that I donβt want to specify any parameters in the action method. So, for example, if I have this controller:
public class HomeController : IController { public Index(int Id) { .. } }
I would like to call the method as follows:
CreateUrl<HomeController>(x=>x.Index);
The signature of the method I came up with is:
public string CreateUrl<TController>(Expression<Action<TController>> action) where TController : IController
But this does not solve the problem of missing parameters. My method can only be called using the parameter specified as follows:
CreateUrl<HomeController>(x=>x.Index(1));
Is it possible to specify an action or method on the controller without having to set parameters?
source share