NoviceToDotNet
NoviceToDotNet

Reputation: 10805

what is the main point to enter in asp.net?

Like in java the entry point is public static void main(String[] args). What is the entry point in ASP.NET using C#? Usually, I see the page load method, is that the entrance point?

Does asp.net follows some different criteria?

Upvotes: 2

Views: 4868

Answers (3)

Joel Coehoorn
Joel Coehoorn

Reputation: 415880

There is no "main point" in asp.net. What you would think of as "main" in asp.net is code that's already written for you. Instead, you inherit a base class ("Page"). As part of this, you can (but don't have to) implement several event handlers. Through the process of building a page, Asp.Net will raise these events for you to handle. The process of running through these events in order is called the page lifecycle.

For your case, there are several options depending on what you want the main method to do:

  • Handle the Application_Start event in the Global.asax file
  • The Page_PreInit event (the very first event in the page life cycle)
  • The Page_Load event (the most common event handled in the page life cycle)

Upvotes: 7

usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

You said something wrong.

public static void main() is a Java method too, used as entry point for console applications the exact way C# does.

You might want to compare servlets/JSP and ASP.NET, don't you?

Servlets vs IHttpHandler

They are, conceptually, the same thing. They are also both interfaces. Their configuration is different (WEB.xml VS Web.config or .ashx file), but their entry points are "almost" the same.

Servlet:

Init()
Service()
Destroy()

IHttphandler:

ProcessRequest() <<--- does all the things
IsReusable {get;} <<--- optional

JSP vs ASP.NET pages

If you define a constructor, or override the InitializeFramework() method, then you have a starting point (or, at least, a breakpoint to put at the almost-very-beginning of the execution), but not an entry point.

Page class implements IHttpHandler, if you allow me some Java syntax in .NET world, but you don't see anything. You might want to go deeper into page life cycle as linked by other users. Basically explaining, Page encapsulates its complete life cycle in events, that resemble clock ticks when you work with VHDL components.

Execution is not concurrent as it seems, but since you can't know the exact order in which controls will raise the same event, you can go as the VHDL example in which you can't read the value of a registry before the next clock tick.

There are several events: here are the most important in their execution order

  • Init: page is initialized, GET, POST and cookie data are available. If you override, then you should initialize your webapp context (ie setup db connections)
  • Load (PreLoad and LoadComplete too): page loads the UI data and restores, if needs to, the state of controls displayed on the page. If you set up DB connections in init, you shouldn't use them before PreLoad to be sure you don't get an exception. The same applies to sequence PreLoad->Load->LoadComplete.
  • DataBind: data-bound controls load data from database, file or whatever (ie. tables get the data to display)
  • Validation: if you use validators, their business logic is processed to determine whether the page is valid or not. No further explaining here
  • Postback processing: if you click a button, then its server-side code is executed
  • PreRender (and PreRenderComplete): the page is getting ready for being rendered into HTML. Usually stores internal data into a collection named ViewState which I won't explain any further here. Usually you would finalize some data-related operations and/or decide whether to render or not some controls on the page according to the page's state. For example, if you have a CAPTCHA and the user solved the puzzle, you won't render it again
  • Render: not actually a programmatic event, but the page gets rendered to HTML
  • Dispose: resources get freed, as occurs with Destroy in Java

Upvotes: 3

IrishChieftain
IrishChieftain

Reputation: 15253

You need to take a look at the ASP.NET life-cycle:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Upvotes: 8

Related Questions