0

I know the feature of App_Code folder in website project, however if I want to add this folder in Web Application Project the purpose remains same, i.e I want to put some un-compiled code in my application.

I am aware of "Build Action" option in file properties, if we set it to "Compile", Visual Studio will compile the code (that is in App_Code folder) so the purpose fails. But if I keep "Build Action" to "Content" (it make sense) then I can't access classes in this folder from any other part of the application.

How can I access this code from outside of App_Code folder if I keep files "Build Action" to "Content"?

Lali
  • 2,816
  • 4
  • 30
  • 47
  • What are you trying to accomplish? – Gabriel GM Dec 24 '14 at 19:40
  • I want to access code in App_Code folder. I also want to keep "Build Action" to "Content". – Lali Dec 24 '14 at 19:47
  • I'm sorry, what you're asking makes no sense. What is the purpose of code that doesn't get compiled? If it doesn't get compiled, what do you think it will _do_? – John Saunders Dec 24 '14 at 21:21
  • @John, what I need is written in your comment. "What is the purpose of code that doesn't get compiled?". I added App_Code folder (from asp.net folders) in WEB APPLICATION PROJECT and then created a class file in this folder. I can't access code in this file, why? The nature of the file ("Build Action" property value "Content") says that this code will not be compiled, then what is its purpose in Web Application Project. – Lali Dec 25 '14 at 11:03
  • 1
    It has no purpose in a web application project. It is not used in web application projects. You can't access the code in the file in `App_Code` _because_ it doesn't get compiled. Code files must be compiled before they are code. Otherwise they're just text files with no meaning. – John Saunders Dec 25 '14 at 15:44
  • 1
    ... Or ressources needed for other actions where your program reads them at runtime. Build Action "Content" for template files, config files, etc. – Gabriel GM Dec 26 '14 at 03:30

2 Answers2

0

I don'T think it's standard.. Depending on your need, you can use .cs (assuming C#) and build action compile without creating a dll. The first request to the web server is gonna compile the website for you.

If you don't want the webserver to recompile the website everytime you change a file in the app_code, you're gonna have to use a scripting approach (you can refer to https://stackoverflow.com/.../what-is-the-best-scripting-language-to-embed-in-a-c-sharp-desktop-application). There are plenty of ressources about scripting, so I won't give example here.

Community
  • 1
  • 1
Gabriel GM
  • 6,391
  • 2
  • 31
  • 34
-2

You can access any content file in your application by reflecting on the assembly and getting the data that way. I usually use a tool like reflector from redgate to see the file and how it is named. Then, I use code like the following to actually read the file into a string.

Here is the basic code copied from another answer.

How to read embedded resource text file

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
  string result = reader.ReadToEnd();
} 
Community
  • 1
  • 1
Peter Kellner
  • 14,748
  • 25
  • 102
  • 188