Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under ASP.Net MVC v1, is Page_Load called?

Stupid Monday morning basic/noob question...

Is Page_Load generally called for an ASP.Net MVC app?

We just have one page, Default.aspx.cs - which only seems to be called for / and not for any sub-paths, so I dont think it is generally called...

Thanks for the reply - to clarify, what I want to do in the Page_Load is the security checks, ie is user logged on/authorised for the page... Sounds like I should do a custom attribute and put it on a Controller base class instead.

Thanks, Chris

like image 913
Chris Kimpton Avatar asked Jan 21 '23 23:01

Chris Kimpton


1 Answers

In theory it is, because a view inherrits from ViewPage, which inherrits the events from Page. However it will be the Load event (it probably wont be automatically wired to Page_Load).

However, you almost certainly don't want to do this! In a normal MVC application there is no concept of code behind. Views (or anything 'behind' them) should not contain logic. This should go into your controller in small applications, or moved out into other layers in larger apps. The only reason you might want to do this is integration with some existing non-MVC apps, but even then it's very debatable.

UPDATE:

For security, yes you are on the right lines with your other suggestions. (You definately don't want to use Page_Load for this). MVC provides the AuthorizationAttribute out of the box. You can apply it to controller classes (or base classes), and individual actions, and can specify authorised roles. It's fine for most scenarios. If you wanted to do something custom you could create a custom attribute.

like image 171
UpTheCreek Avatar answered Jan 31 '23 04:01

UpTheCreek