I'm trying something very simple - call a c# function from a razor macro, both defined in the same template - could someone explain why this gives a 'does not exist in the current context' error for the testMe method...
Thanks for your reply Jeroen, seems crazy you can't call a c# function from Razor when declared in the same page (but you can if using code behind). Is this a failure of Umbraco or the .NET framework?
Well the Umbraco Macro is just different from using a server script. You could create a static class and add your method there. Than you can call that static class in razor (after adding the namespace). Not sure if that's what you want?
> You could create a static class and add your method there. Where in Umbraco would I do this? I'd like to maintain any macros within Umbraco (through the browser) otherwise I'd need permanent access to the server. I need to administer the site from anywhere, so do not always have development applications available (VS, FTP etc.) on the move.
I always add a static class to my dll and include that, but looks like that's not what you want. In that case you could create a .cs file in the App_Code folder. I think that will work, but you still can't edit it inside Umbraco. If you want to manage everything from inside Umbraco it's best to use my first example.
Ok, I'll have to rewrite all my code in Razor to edit outside a dev environment. Shame. Means I need to upgrade too, as 4.6 razor is buggy. Thanks for your help!
How is your code currently written and why do you want to change everything in Umbraco? We usually let content editors change things in Umbraco, but for real developer stuff it's better to just use Visual Studio and copy it over in a dll when you're done. Why won't you do that?
Call C# from Razor - what's wrong with...
I'm trying something very simple - call a c# function from a razor macro, both defined in the same template - could someone explain why this gives a 'does not exist in the current context' error for the testMe method...
<%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" %>
<script runat="server">
public string testMe() {
return ("hello world");
}
</script>
<asp:Content ContentPlaceHolderId="ContentPlaceHolderDefault" runat="server">
<umbraco:Macro runat="server" language="razor">
@testMe()
</umbraco:Macro>
</asp:Content>
Don't think those can communicate. Try this:
Jeroen
Thanks for your reply Jeroen, seems crazy you can't call a c# function from Razor when declared in the same page (but you can if using code behind). Is this a failure of Umbraco or the .NET framework?
Well the Umbraco Macro is just different from using a server script. You could create a static class and add your method there. Than you can call that static class in razor (after adding the namespace). Not sure if that's what you want?
Jeroen
Hi Jeroen, and thanks again,
> You could create a static class and add your method there.
Where in Umbraco would I do this? I'd like to maintain any macros within Umbraco (through the browser) otherwise I'd need permanent access to the server. I need to administer the site from anywhere, so do not always have development applications available (VS, FTP etc.) on the move.
I always add a static class to my dll and include that, but looks like that's not what you want. In that case you could create a .cs file in the App_Code folder. I think that will work, but you still can't edit it inside Umbraco. If you want to manage everything from inside Umbraco it's best to use my first example.
Jeroen
> best to use my first example.
Ok, I'll have to rewrite all my code in Razor to edit outside a dev environment. Shame. Means I need to upgrade too, as 4.6 razor is buggy. Thanks for your help!
How is your code currently written and why do you want to change everything in Umbraco? We usually let content editors change things in Umbraco, but for real developer stuff it's better to just use Visual Studio and copy it over in a dll when you're done. Why won't you do that?
Jeroen
is working on a reply...