This project is read-only.

View Renders Differently When Compiled

Sep 7, 2012 at 4: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.

Sep 10, 2012 at 5: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 5: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 3:49 PM

Was this fixed by the following changeset?:

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

Oct 14, 2012 at 4:27 PM
Edited Oct 14, 2012 at 4: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 5: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 9:22 PM

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

Although I am referencing System.Web.Razor version, 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 9: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=" 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?

Oct 15, 2012 at 4: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 17, 2012 at 12:26 AM

Here's what I had in my project file:

<Reference Include="System.Web.Razor">

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

<Reference Include="System.Web.Razor, Version=">
Oct 17, 2012 at 12:27 AM

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.