C# Include Files/Content


I know it's not necessarily best practice, but I have a very specific need to include and a block of code across many files on a C# website. Does anyone know how to work that out?

Full disclosure - I'm fixing some hand-coded code, so it's good fun. I'm also coming from a PHP background, so go easy on me - there's no Visual Studio here and I'm looking for down and dirty.

Thank you!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PawełI Design & Develop SoftwareCommented:
do you mean for the UI because you could make a custom control

if it's for the data model then you'd use inheritance

if you need to create a data handler or some sort of helper class with functions  i'd either make a static class or use the singleton pattern
Robberbaron (robr)Commented:
code classes placed in the App_Code folder of the website are available to all pages.

I have code there that sends a formated email , called from about 15 separate pages.

it may be worth installing VS2015 Community (it;s free) and pulling the website into that for editing.
RSPANGLER_AVTIAAuthor Commented:

Sorry for my delay - I spent some time investigating everything, and I believe that the App_Code folder is the way to go here - that makes the most sense.

I'm going to investigate that process today, then I'll close this up if I don't have more questions. In the meantime, any tips on calling code (or anything that you need to know when storing code in the App_Code folder) are appreciated!!

Thank you both so much!
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

RSPANGLER_AVTIAAuthor Commented:
Hi guys, do you see any reason why it would be bad to use classic include files? This would be even easier for the team I'm working with, and I'm looking for clean, stable, and easy:

<!--#include file="inc/footer.ascx" -->

I know that's bigtime legacy stuff, but if it works and it's stable...

RSPANGLER_AVTIAAuthor Commented:
One last note - I think the above legacy method does not work for things like database connections, etc. because include files called this way execute with the HTML, or after the C# code... just looking for someone to confirm or deny that, thank you!!
Robberbaron (robr)Commented:
In a Web site project, you can store source code in the App_Code folder, and it will be automatically compiled at run time.

with the #include, the code is parsed with each page load, where code files in App_Code are compiled once for the website.

so the App_Code folder is more efficient at runtime.

I have almost completed converting an old ASP1 website app to ASP.NET4 and found the old  #include made the code hard to read and break any Intellisense regarding function definitions and the like. I've converted them to CodeClasses in App_Code folder. (They were and still are VB code so much like static C# classes)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RSPANGLER_AVTIAAuthor Commented:
Really appreciate your feedback/support - this is exactly what I needed.

Thank you!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.