0

I was following some tutorials and created a new C# .Net Framework 4.6 Application as a web form type. When working on the project I noticed it doesn't have a Main Function to add logic too, it also doesn't have a program.cs, yet it is able to build fine. Why is that, I was under the impression that all C# programs need a main function to start as there entry point, and have seen multiple resources on the web stating that(https://www.c-sharpcorner.com/blogs/main-method-in-c-sharp, https://www.completecsharptutorial.com/basic/main-method.php).

If we don't need it what is replacing it, and serving as the code entry point. I also noticed that there is no program.cs what is the reason for this. I have been researching but I find the answers posted very contradictory to what is actually in the code.

nightwolf555
  • 327
  • 1
  • 14
  • All programs need to have an entry point. By default this is a static function called `Main` (although it doesn't have to be in a class called `Program`: that's just convention), but you can configure the compiler to call any method you like as the entry point. Project -> Properties -> Application -> Startup object. My guess is either that this is set to something different (or it just defaults to something different in a webforms project), or the main method is being generated when the project is compiled, so you don't see it in the source code. – canton7 Nov 19 '21 at 18:12
  • The very first line of the article in your first link is "The Main method is the entry point of a C# console application or windows application." A Web form is not a console or windows desktop application; it is a web page – Mark Benningfield Nov 19 '21 at 18:12
  • so where does it start from when it is type web page? – nightwolf555 Nov 19 '21 at 18:13
  • 1
    It doesn't start, per se. It becomes a DLL that has all the code of the app, and the DLL is loaded by a program (such as IIS web server) that is already running. When someone makes a request to IIS, IIS calls different functions inside the DLL(s) comprising the app. You could consider that an ASP.NET webforms app is "just a plugin for IIS that makes it behave in a very specific way". IIS might call certain functions at certain times, such as "when it loads the dll" which is like a startup, and would be the closest thing to a Main method corollary in a console app – Caius Jard Nov 19 '21 at 18:29

3 Answers3

1

Because in the old (.NET Framework) it runs in process - the startup code is for some reason that are MOSTLY historical (ASP pre .NET) in a file GLOBAL.ASAX

What is the purpose of global.asax in asp.net

Again, this really goes back to asp.net mvc following up on the concepts of asp (pre .net). It is rectified in the Core versions where basically a program starts for the application.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • so if I wanted to add some that I would normally put in the main, where would I put it, would the global.asax be the correct place? – nightwolf555 Nov 19 '21 at 18:18
  • Why don't you just ask "I have this code X where should I put it in my asp net project?". How can we accurately say "yes, that code you say you have that we can't see, should definitely go in global.asax" ? – Caius Jard Nov 19 '21 at 18:27
  • the global fiule has event handlers for start and stop. Obviously one of them (start) is where startup code goes. – TomTom Nov 19 '21 at 18:29
1

ASP.NET applications do have an entry point but in a roundabout way.

For a .NETFramework ASP.NET Web Forms app, you are usually going to have a Global.aspx.cs file with an Application_Start method that does your application configuration (routes etc).

See https://learn.microsoft.com/en-us/previous-versions/ms178473(v=vs.140)?redirectedfrom=MSDN#life-cycle-events-and-the-globalasax-file

Hank
  • 1,976
  • 10
  • 15
1

A web form or app is not a "program". It is a collection of pages and other classes that are referenced by the underlying ASP.NET service.

There are many more details to the ASP.NET ecosystem, but the main reason is because a site is not a "program" that needs an entry point. Only console apps and windows apps need an entry point.

D Stanley
  • 149,601
  • 11
  • 178
  • 240