Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jed 55 posts 80 karma points
    Jan 19, 2012 @ 16:40
    Jed
    0

    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>

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 19, 2012 @ 16:52
    Jeroen Breuer
    0

    Don't think those can communicate. Try this:

    <umbraco:Macro runat="server" language="razor">
    @testMe()
    
    @functions 
    {
       string testMe() {
          return ("hello world");
       }
    }
    </umbraco:Macro>

    Jeroen

  • Jed 55 posts 80 karma points
    Jan 19, 2012 @ 17:35
    Jed
    0

    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?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 20, 2012 @ 00:51
    Jeroen Breuer
    0

    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

  • Jed 55 posts 80 karma points
    Jan 20, 2012 @ 13:33
    Jed
    0

    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.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 20, 2012 @ 14:03
    Jeroen Breuer
    0

    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

  • Jed 55 posts 80 karma points
    Jan 20, 2012 @ 17:32
    Jed
    0

    > 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! 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 20, 2012 @ 17:39
    Jeroen Breuer
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft