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.
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.
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.
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:
I am trying to access this in Razor by doing the following:
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.Hi Kulvir,
It should work fine, I just tried with this code and it worked fine:
/App_Code/Test.cs
Then in my view:
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.
Hi Kulvir,
Only thing that comes to mind is to check your folder permissions are all set correctly?
Jeavon
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.
If you right click on the class in the app folder, I believe you have to set it to compile instead of content.
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.
Hi,
which IDE do you use to select this property? Visual Studio Code doesn't offer this functionality.
Thank you.
Nadine
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
Change this to
I don't have this file in my project, because I didn't created a web project. It suffice to restart the IIS server
is working on a reply...