I'm evaluating Razor im Umbraco 4.7 for upgrading an existing site from 4.5. I want to rewrite some macros in Razor, if it works, that is. I'm currently testing extensions methods and I have run into a problem. As stated in this blog post (http://umbraco.com/follow-us/blog-archive/2011/2/28/umbraco-razor-feature-walkthrough-%E2%80%93-part-3), all you have to do is put this in a class library and put it in the /bin folder. That, unfortunately, does not work for me (not even the provided sample code). Has something changed since this blog post was written (it seems to be pretty much the only documentation around for this)?
The site in question is unfortunately confidential, so I cannot really show much code. I will have to make a reduced testcase first (which in any case might be a good idea!)
I've written extension methods for Umbraco 4.7 based on that blog-post and they work fine. Remember that your extension methods MUST go in a static class and you may need to import the namespace. One easy way to use extension methods is to simply place your code in the App_Code folder (at least whilst developing). For instance, here's an entire example of an extension method class I've put in App_Code called RazorExtensions.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using umbraco.MacroEngines;
namespace Diplo { public static class RazorExtensions { public static DynamicNodeList TopNodesByDate(this DynamicNodeList nodes, int amount) { return new DynamicNodeList(nodes.Items.OrderBy(d => d.CreateDate).Take(amount)); } } }
Good explanation Dan, works for me! Don't actually need the namespace and some of the usings:
using System.Linq; using umbraco.MacroEngines;
public static class RazorExtensions { public static DynamicNodeList TopNodesByDate(this DynamicNodeList nodes, int amount) { return new DynamicNodeList(nodes.Items.OrderBy(d => d.CreateDate).Take(amount)); } }
I'm experiencing the same issue. I have placed the following in the project App_Code folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.MacroEngines;
namespace Dool {
public static class RazorExtensions {
public static bool ContainsAny(this string haystack, List needles) {
if (!string.IsNullOrEmpty(haystack) || needles.Count > 0) {
foreach (string value in needles) {
if (haystack.Contains(value)) { return true; }
}
}
return false;
}
}
}
And below is my Razor script
@inherits umbraco.MacroEngines.DynamicNodeContext
@using uComponents.Core
@using uComponents.Core.uQueryExtensions
@using System.Linq
@{
var rootNode = uQuery.GetRootNode();
var searchString = HttpContext.Current.Request.QueryString["SearchTerm"];
var keywords = HttpContext.Current.Request.QueryString["SearchTerm"].Split(' ').ToList();
var values = new Dictionary();
values.Add("keywords", keywords);
var pagesToList = rootNode.GetDescendantNodes().Where(x =>
x.Name.ToLower().Contains(searchString) ||
x.GetPropertyAsString("bodyText").ToLower().ContainsAny(keywords)
);
...
And this is the error that is appearing.
'string' does not contain a definition for 'ContainsAny' and no extension method 'ContainsAny' accepting a first argument of type 'string' could be found
The Umbraco example, calls ContainsAny like follows
var items = @Model.Children.Where("Name.ContainsAny(keywords)", values);
Any ideas on how I correctly call ContainsAny with the syntax that I am using?
you will need to add (for your example extension method)
@using Dool
to your razor file, you need to register the namespace to your extension method class for c# to be able to see it and run the method, thi goes for normal classes to.
Razor extension methods
I'm evaluating Razor im Umbraco 4.7 for upgrading an existing site from 4.5. I want to rewrite some macros in Razor, if it works, that is. I'm currently testing extensions methods and I have run into a problem. As stated in this blog post (http://umbraco.com/follow-us/blog-archive/2011/2/28/umbraco-razor-feature-walkthrough-%E2%80%93-part-3), all you have to do is put this in a class library and put it in the /bin folder. That, unfortunately, does not work for me (not even the provided sample code). Has something changed since this blog post was written (it seems to be pretty much the only documentation around for this)?
The site in question is unfortunately confidential, so I cannot really show much code. I will have to make a reduced testcase first (which in any case might be a good idea!)
Hi,
I've written extension methods for Umbraco 4.7 based on that blog-post and they work fine. Remember that your extension methods MUST go in a static class and you may need to import the namespace. One easy way to use extension methods is to simply place your code in the App_Code folder (at least whilst developing). For instance, here's an entire example of an extension method class I've put in App_Code called RazorExtensions.cs:
And now here's an example of it in use:
Good explanation Dan, works for me! Don't actually need the namespace and some of the usings:
Thanks, Seb. True, you don't need those namespaces but Visual Studio likes to add them and who am I to argue with M$? :)
I'm experiencing the same issue. I have placed the following in the project App_Code folder
And below is my Razor script
And this is the error that is appearing.
'string' does not contain a definition for 'ContainsAny' and no extension method 'ContainsAny' accepting a first argument of type 'string' could be found
The Umbraco example, calls ContainsAny like follows
Any ideas on how I correctly call ContainsAny with the syntax that I am using?
you will need to add (for your example extension method)
to your razor file, you need to register the namespace to your extension method class for c# to be able to see it and run the method, thi goes for normal classes to.
Thanks Scott Williams - exact answer I was looking for
is working on a reply...