I am trying to write a helper in Razor that looks like the following:
@helper DoSomething<T, U>(Expression<Func<T, U>> expr) where T : class
Unfortunately, the parser thinks that <T is the beginning of an HTML element and I end up with a syntax error. Is it possible to create a helper with Razor that is a generic method? If so, what is the syntax?
No, this is not currently possible. You could write a normal HTML helper instead.
and then:
or if you are targeting the model as first generic argument:
which will allow you to invoke it like this (assuming of course that your view is strongly typed, but that’s a safe assumption because all views should be strongly typed anyways :-)):