Folks,
We are trying to use the strongly typed action link methods that look like this:
Html.ActionLink<HomeController>
in the Razor view engine.
I know we shouldn’t use them all the time because it ignores filters, etc., but the fact is we do use them.
If I try to use this directly in Razor like so:
@Html.ActionLink<HomeController>(c => c.Index, "Home")
I get an error of:
CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
If you look at the compiled code, it’s because Razor is not parsing that statement as you would expect. The compiled source, from the error that has the line looks like this:
...
Line 101: #line 13 "C:\dev\TheNetwork\POC\Web\Views\Policy\Edit.cshtml"
Line 102: Write(Html.ActionLink);
Line 103:
Line 104:
Line 105: #line default
Line 106: #line hidden
Line 107: WriteLiteral("<PolicySectionController>(c => c.Edit(null), "New\")\r\n\r\n\r\n\r\n");
Much stuff omitted for brevity 🙂 As you can see, it splits it on the “<” I think it’s interpreting that as an HTML tag, but I can’t be sure.
I found a workaround, but it’s ugly. This works:
@{Write(Html.ActionLink<PolicySectionController>(c => c.Edit(null), "New"));}
Does anyone know of a better way to do this?
Yeah, in order to use generic methods you need to escape the expression using parens. Would this work: