Razor script errors after upgrading from 4.7 to 6.1.6
I have inherited a site that I'm trying to upgrade from umbraco 4.7 to 6.1.6. Everything's gone well, but I have a bunch of cshtml razor scripts that are throwing errors when they worked fine in the older version of umbraco. I'm wondering if, in upgrading, that the processing of the scripts works differently in the newer version and the syntax of the older scripts is no longer compatible. Can anyone tell me how I can tell if this is the case or if it's something else?
The Razor implementation has changed between versions. Originally version 4.7.x of Umbraco was supposed to be the last since v5 was in the making. But as you might know it got canned and development continued with v4 so v4.8 to v4.11.x was released untill v6 was ready last year around this time. So A LOT has happened under the hood between versions.
I did not follow the upgrade instructions there. I did a clean install of 6.1.6, and let NuGet update packages where I could before porting the old code (including all our cshtml scripts). Here's an example of one that is broken now:
this bit of code:
foreach (var item in nodeIdList) { <li> @try { var HARDCODED_Home_Aboutus_Press = 1147; var pressUrl = (new DynamicNode(HARDCODED_Home_Aboutus_Press)).Url; var node = Model.NodeById(item); var date = node.pressReleaseDate.ToString("MM/dd/yyyy"); <span>@date</span> @WilshireHelpers.RenderPressLink(node, pressUrl) } catch (Exception ex) { @ex.Message } </li> }
var link = string.Empty; var newWindow = string.Empty; if (!string.IsNullOrEmpty(pressNode.pressReleaseLink)) { string[] lnk = pressNode.pressReleaseLink.Split(','); if(lnk[1].ToLower() == "true") { newWindow = " target=\"__blank\"";} if(lnk[0] == "Content") { if(!string.IsNullOrEmpty(lnk[2])) { link = pressUrl + "?id=" + pressNode.Id.ToString(); } } else { if(!string.IsNullOrEmpty(lnk[3])) { link = lnk[3]; } } } <a href="@link">@pressNode.pressReleaseTitle</a> }
which produces this error on the page:
Could not load type 'Xml' from assembly 'uComponents.Core, Version=3.0.2.1, Culture=neutral, PublicKeyToken=null'.
What it appears is that while it grabs the node in the original function using Model.NodeById(item), that when trying to access document properties in that node in the helper function call to @pressNode.pressReleaseTitle it fails. I worked around this in another area where I had a similar issue by not trying to get the property directly with a node.property syntax but instead used node.GetProperty("property_name").Value or node.GetPropertyValue("property_name"). That said, I'd rather not hack each script to do this, and want to know what the right/clean solution to the problem is.
No sure if this sounds silly but can you assert that all of the code works? I.E not null and all of the properties RenderPressLink() are correct and not errroing. Is ink[] not String.Empty?
Also why is you paramter 'dynamic'? You will lose all intellisense?
The only thing I can attest to is that the scripts worked against the same dataset when running umbraco 4.7. As for the parameter, I'm not all that familiar with .cshtml scripting yet, since I inherited this code, so can't say one way or another why coding choices were made. :-)
Razor script errors after upgrading from 4.7 to 6.1.6
I have inherited a site that I'm trying to upgrade from umbraco 4.7 to 6.1.6. Everything's gone well, but I have a bunch of cshtml razor scripts that are throwing errors when they worked fine in the older version of umbraco. I'm wondering if, in upgrading, that the processing of the scripts works differently in the newer version and the syntax of the older scripts is no longer compatible. Can anyone tell me how I can tell if this is the case or if it's something else?
Hi Neil
The Razor implementation has changed between versions. Originally version 4.7.x of Umbraco was supposed to be the last since v5 was in the making. But as you might know it got canned and development continued with v4 so v4.8 to v4.11.x was released untill v6 was ready last year around this time. So A LOT has happened under the hood between versions.
So could you give us some examples of the errors you're seeing now? And did you follow the upgrade documentation here http://our.umbraco.org/documentation/Installation/Upgrading/ ?
/Jan
Hi Jan,
I did not follow the upgrade instructions there. I did a clean install of 6.1.6, and let NuGet update packages where I could before porting the old code (including all our cshtml scripts). Here's an example of one that is broken now:
this bit of code:
foreach (var item in nodeIdList)
{
<li>
@try
{
var HARDCODED_Home_Aboutus_Press = 1147;
var pressUrl = (new DynamicNode(HARDCODED_Home_Aboutus_Press)).Url;
var node = Model.NodeById(item);
var date = node.pressReleaseDate.ToString("MM/dd/yyyy");
<span>@date</span>
@WilshireHelpers.RenderPressLink(node, pressUrl)
}
catch (Exception ex)
{
@ex.Message
}
</li>
}
calls this helper function:
@helper RenderPressLink(dynamic pressNode, string pressUrl){
var link = string.Empty;
var newWindow = string.Empty;
if (!string.IsNullOrEmpty(pressNode.pressReleaseLink)) {
string[] lnk = pressNode.pressReleaseLink.Split(',');
if(lnk[1].ToLower() == "true") { newWindow = " target=\"__blank\"";}
if(lnk[0] == "Content") {
if(!string.IsNullOrEmpty(lnk[2])) { link = pressUrl + "?id=" + pressNode.Id.ToString(); }
} else {
if(!string.IsNullOrEmpty(lnk[3])) { link = lnk[3]; }
}
}
<a href="@link">@pressNode.pressReleaseTitle</a>
}
which produces this error on the page:
Could not load type 'Xml' from assembly 'uComponents.Core, Version=3.0.2.1, Culture=neutral, PublicKeyToken=null'.
What it appears is that while it grabs the node in the original function using Model.NodeById(item), that when trying to access document properties in that node in the helper function call to @pressNode.pressReleaseTitle it fails. I worked around this in another area where I had a similar issue by not trying to get the property directly with a node.property syntax but instead used node.GetProperty("property_name").Value or node.GetPropertyValue("property_name"). That said, I'd rather not hack each script to do this, and want to know what the right/clean solution to the problem is.
No sure if this sounds silly but can you assert that all of the code works? I.E not null and all of the properties RenderPressLink() are correct and not errroing. Is ink[] not String.Empty?
Also why is you paramter 'dynamic'? You will lose all intellisense?
Charlie :)
Hi Charlie,
The only thing I can attest to is that the scripts worked against the same dataset when running umbraco 4.7. As for the parameter, I'm not all that familiar with .cshtml scripting yet, since I inherited this code, so can't say one way or another why coding choices were made. :-)
is working on a reply...