开发者

Under ASP.Net MVC v1, is Page_Load called?

开发者 https://www.devze.com 2023-01-01 12:50 出处:网络
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-pa

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

Tha开发者_如何学Cnks 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消