Does the Razor View Engine Support Generic Methods? September, 2010
Trying out the Razor view engine and for some reason my generic helper methods are breaking. For example:
public static class UrlHelperExtensions { public static string NonGenericHelper(this UrlHelper helper, Type controller) { return controller.Name; } public static string GenericHelper<TController>(this UrlHelper helper) { return typeof(TController).Name; } }
Works as expected:
@Url.NonGenericHelper(typeof(ProjectEuler.UI.Models.Home))
Breaks with the following exception:
@Url.GenericHelper<ProjectEuler.UI.Models.Home>()
"CS1502: The best overloaded method match for 'Microsoft.WebPages.WebPageUltimateBase.Write(Microsoft.WebPages.Helpers.HelperResult)' has some invalid arguments"
The only workaround I could find is treating it as a "multi-token statement":
@(Url.GenericHelper<ProjectEuler.UI.Models.Home>())
Not sure if this is the intended behavior or just a bug but it would be nice to be able to call a generic method as you would a non generic one.
Update: Brad Wilson mentions the following: "That is the intended behavior, because otherwise the parser would not be able to tell the difference between a generic and a piece of HTML content. Using the surrounding ( ) disambiguates it."