C# Include Files/Content

RSPANGLER_AVTIA
RSPANGLER_AVTIA used Ask the Experts™
on
Hello,

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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PawełI Design & Develop Software
Commented:
do you mean for the UI because you could make a custom control
https://msdn.microsoft.com/en-us/library/zt27tfhy.aspx

if it's for the data model then you'd use inheritance
https://msdn.microsoft.com/en-us/library/ms173149.aspx

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
https://msdn.microsoft.com/en-us/library/ff650316.aspx
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.

Author

Commented:
Hello!

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 ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

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...

Thanks!

Author

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!!
In a Web site project, you can store source code in the App_Code folder, and it will be automatically compiled at run time.
https://msdn.microsoft.com/en-us/library/t990ks23.aspx?f=255&MSPPError=-2147217396
http://vishaljoshi.blogspot.com.au/2009/07/appcode-folder-doesnt-work-with-web.html

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)

Author

Commented:
Really appreciate your feedback/support - this is exactly what I needed.

Thank you!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial