This project is read-only.

Application relative paths in templated razor delegates are not resolved properly

Mar 11, 2013 at 10:18 PM
I don't know if anyone else is seeing this, but we have a custom html helper that takes a templated delegate to keep track of all of the script tags we need in our views and partials and then place them all at the bottom of our page. After installing RazorGenerator.Mvc and RazorGenerator.MsBuild we're seeing that application relative paths are not being resolved and being treated as normal relative paths. For example, in a view in our admintools area we try to use the ~/Scripts/Users/UserCommon.js script, which usually resolves to /WebUI/Scripts/Users/UserCommon.js but instead resolves to WebUI/admintools/~/Scripts/Users/UserCommon.js.

If it helps, our HtmlHelper extension has the following signature:
public static MvcHtmlString Script(this HtmlHelper htmlHelper, Func<object, HelperResult> template)
And is used like this (with a call to Html.RenderScripts in our _layout.cshtml):
@Html.Script(@<script type="text/javascript" src="~/Scripts/Users/UserCommon.js"></script>)
Upon inspecting the generated code we see that the script tag in the template is just treated as a string,
Write(Html.Script(item => new System.Web.WebPages.HelperResult(__razor_template_writer => {
WriteLiteralTo(@__razor_template_writer, "<script type=\"text/javascript\" src=\"~/Scripts/Users/UserCommon.js\"></script>");
Where outside of the templated delegate it works just peachy. Any ideas?
Mar 11, 2013 at 10:23 PM
Haven't look at the details, but first step would be to determine whether what you're seeing is specific to RazorGenerator, or whether it happens when using Razor without RazorGenerator.
Mar 11, 2013 at 10:25 PM
It looks like the MsBuild task isn't able to identify that the project is targeting Mvc 4 \ WebPages 2.0. Can you verify that your project references include a reference to WebPages 2.0?
Mar 12, 2013 at 3:01 PM
Edited Mar 12, 2013 at 3:02 PM
Hey guys, I've verified that it does work without RazorGenerator and that the project references WebPages and Razor 2.0. I also took the IsMvcProject method and ran it in a console app against the project directory, it detected version 2.