Copied to clipboard

Flag this post as spam?

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


  • Kal 13 posts 71 karma points
    Aug 13, 2013 @ 16:19
    Kal
    0

    Access App_Code classes in Razor

    I am new to Umbraco, from a .net C# background. I am using Umbraco 6.1.2. I have a class in the AppCode folder which I am trying to access from razor. I know about the using statement to the namespace but it doesnt recognize it. For example, I have the following in a class in the AppCode folder:

    namespace Test
    {
      public class DoWork
      { 
         public string Title {get; set;}
         public static string FirstTask()
         {  
            this.Title = "Hello";
            return this.Title; 
         }
      }
    }
    

    I am trying to access this in Razor by doing the following:

    using Test;
    using ...
    string response = FirstTask();
    

    The code does not recognize Test in the using statement. It can't see it at all. If I do Test.DoWork.FirstTask(), I get the same issue (The name 'Test' does not exist in the current context).Any ideas what I could be doing wrong? I've seen this used before but don't know why it doesnt work now. The only difference is the working solutuion was Umbraco WebForms 4.11 and my solution is 6.1.2 MVC.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 13, 2013 @ 18:22
    Jeavon Leopold
    0

    Hi Kulvir,

    It should work fine, I just tried with this code and it worked fine:

    /App_Code/Test.cs

    namespace Test
    {
        public class DoWork
        {
            public string Title { get; set; }
            public static string FirstTask()
            {
                var Title = "Hello";
                return Title;
            }
    
        }
    }
    

    Then in my view:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Test;
    @{
        Layout = "MasterPage.cshtml";
    }
    <p>@DoWork.FirstTask()</p>
    
  • Kal 13 posts 71 karma points
    Aug 14, 2013 @ 10:23
    Kal
    0

    Hi Jeavon,

    You are right, the code works but for me it doesn't work in the project. It's working fine in other projects. Don't understand why? It just doesn't see the namespace created in the App_Code folder.

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 14, 2013 @ 10:28
    Jeavon Leopold
    0

    Hi Kulvir,

    Only thing that comes to mind is to check your folder permissions are all set correctly?

    Jeavon

  • Kal 13 posts 71 karma points
    Aug 14, 2013 @ 11:12
    Kal
    0

    Hi Jeavon,

    I don't know how or why but it's started working! What I did was create a folder called Models and tried to add a class to the folder. This then came back with a warning that I should create the class in the App_Code folder. I clicked OK and it created the class in App_Code. I then tried creating an instance of the class in the razor code and it worked. The class was visible.

    Don't know what was going on but probably the project had some wires crossed somewhere.

    Thanks for your input.

  • Jerode 44 posts 159 karma points
    Aug 18, 2013 @ 19:37
    Jerode
    2

    If you right click on the class in the app folder, I believe you have to set it to compile instead of content.

  • Scott Anderson 18 posts 51 karma points
    Jun 25, 2014 @ 06:18
    Scott Anderson
    1

    Thanks Jerode... That fixed the same issue for me.

    Right-click on the class file in App_Code then select Properties. Then in Properties table change Build Action from Content to Compile.

    Cheers Scott.

  • Nadine Fisch 159 posts 429 karma points
    May 30, 2018 @ 13:10
    Nadine Fisch
    0

    Hi,

    which IDE do you use to select this property? Visual Studio Code doesn't offer this functionality.

    Thank you.

    Nadine

  • Jerode 44 posts 159 karma points
    May 30, 2018 @ 13:25
    Jerode
    0

    Nadine, you will may need to manually edit the csproj file then.

    Look inside the file csproj file for your file name. It will likely look like

    <Content Include="App_Code\MyFileName.cs" />
    

    Change this to

    <Compile Include="App_Code\MyFileName.cs" />
    
  • Nadine Fisch 159 posts 429 karma points
    Jun 01, 2018 @ 08:09
    Nadine Fisch
    0

    I don't have this file in my project, because I didn't created a web project. It suffice to restart the IIS server

Please Sign in or register to post replies

Write your reply to:

Draft