View Renders Differently When Compiled

Sep 7, 2012 at 3:57 PM

Some of my views are not rendering correctly when compiled. When an img src or a href uses a tilde, e.g. "~/images", "~/js", sometimes it renders correctly and other times the tilde is kept and the view is broken.

For example, I use the ~ in my _Layout.cshtml and that works fine when compiled. But when I use it in a partial view, it doesn't work.

Do you know what's going on and what's the best way to work around this? Or is this a known issue with an upcoming bug fix? :)

I am using MVC 4 RC if that makes any difference.

Coordinator
Sep 10, 2012 at 4:19 PM

We're using the v1 parser which does not understand the ~/ feature. We'll need to switch to supporting both the v1 and v2 parsers to make this behave correctly.

Sep 10, 2012 at 4:44 PM

Ok, in the meantime I'll switch from using src="~/images..." to src="@Url.Content("~/images...")" which should hold me over until v2 of the parser is supported. :-)

Thanks for the great tool!

Oct 14, 2012 at 2:49 PM

Was this fixed by the following changeset?: http://razorgenerator.codeplex.com/SourceControl/changeset/8a645b2b720f

If yes, how to activate the support for Razor 2?

Coordinator
Oct 14, 2012 at 3:27 PM
Edited Oct 14, 2012 at 3:28 PM

We look at the project references (essentially check what version of System.Web.Razor you're referencing) to determine the parser version to use. For the Mvc project templates in VS, you shouldn't have to make any changes. You can however explicitly specify the version to be used by adding a directive

@* RazorVersion: 1 *@

Edit: Also, you'd need the newer version of the vsix that we've pushed to the extension gallery. I'll have the msbuild package updated soonish.

Oct 14, 2012 at 4:15 PM

After updating my Razor extension, my issue with ~/ has been resolved and I was able to remove my workaround. Thank you!

Oct 14, 2012 at 8:22 PM

I have Razor Generator 1.5 from Visual Studio's Extension Manager.

Although I am referencing System.Web.Razor version 2.0.0.0, Razor 2 is not automatically supported by Razor Generator.
The directive works, but I don't want to add it everywhere.

What else may I be missing?

Oct 14, 2012 at 8:32 PM

I had a look at the generator's source code, and I found out why it doesn't work in my case. My project doesn't explicitly reference "System.Web.Razor, Version=2.0.0.0" but just "System.Web.Razor".

Given this, I know how to make the generator pick Razor 2.
I prefer not to include specific version information in my references. I don't know if you have other options than looking in the project file, though. Aren't you able to determine what exact System.Web.Razor dll is used?

Coordinator
Oct 15, 2012 at 3:13 AM

Do you think you could share the Reference section of your project? In theory, it shouldn't hurt to load the assembly in to our AppDomain and check its version since we're going to load it later. 

Oct 16, 2012 at 11:26 PM

Here's what I had in my project file:

<Reference Include="System.Web.Razor">
  <Private>True</Private>
  <HintPath>..\Shared\packages\Microsoft.AspNet.Razor.2.0.20710.0\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>

Here's what I have so that Razor Generator picks Razor 2:

<Reference Include="System.Web.Razor, Version=2.0.0.0">
<Private>True</Private>
<HintPath>..\Shared\packages\Microsoft.AspNet.Razor.2.0.20710.0\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>
Oct 16, 2012 at 11:27 PM

Note that not including the version number is pointless because the HintPath already includes a version number... I should probably leave the reference as is and not remove version info as I did.