Question

I have tried every solution I have found on stackoverflow but I cannot seem to access a class I created in the App_Data folder.

If I have this class (only showing part of the class):

public class Encryption
{
    public string Encrypt(string plainText, string Key)
   {
       byte[] key = StringToByteArray(Key);
       string encrypted = ByteArrayToHexString(encryptStringToBytes_AES(plainText, key, null));
       return encrypted;
   }
}

I should be able to access it in a code block like:

@{
    Encryption enc = new Encryption();
    var data = enc.Encrypt("hello", "world");
}

But I get an error that type or namespace "Encryption" cannot be found.

I've also tried to import it with a namespace but can't seem to get my app name to show as a namespace either, seems simpler in asp.net mvc.

I tried all the examples I could find for updating my config file, but to no avail. Is there something I need to do that I'm missing here?

Was it helpful?

Solution

I finally found the answer, I had to create an App_Code folder and put the class in there for it to be referencable.

I think this is because I created the app in WebMatrix, not in VS2010. So, the name of my website (MyWebsite) does not equate to a namespace like a project name does in VS2010. That means if I put the Encryption class in a folder called Utilities I can't do this:

@using MyWebsite.Utilities

Note that a WebMatrix app using the Starter Site does not add an App_Code folder automatically.

Well, back to coding!

OTHER TIPS

Classes in App_data aren't compiled. It is for SQL CE, Express databases and application specific data rather than code. Create another folder off the root and move the class file there or put it on the root of your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top