在 ASP.Net MVC v1 下,是否调用 Page_Load?
愚蠢的周一早上基本/菜鸟问题...
Page_Load 通常是为 ASP.Net MVC 应用程序调用的吗?
我们只有一页,Default.aspx.cs - 它似乎只被调用 / 而不是任何子路径,所以我不认为它通常被称为...
感谢您的回复 - 澄清我想要什么在 Page_Load 中要做的是安全检查,即用户是否登录/授权了页面...听起来我应该做一个自定义属性并将其放在控制器基类上。
谢谢, 克里斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理论上是这样,因为视图继承了
ViewPage
,而 ViewPage 又继承了Page
的事件。但是,它将是Load
事件(它可能不会自动连接到Page_Load
)。但是,您几乎肯定不想这样做!在普通的 MVC 应用程序中不存在代码隐藏的概念。视图(或它们“背后”的任何东西)不应包含逻辑。这应该进入小型应用程序中的控制器,或者移出到大型应用程序中的其他层。您可能想要这样做的唯一原因是与一些现有的非 MVC 应用程序集成,但即使如此,这也是非常有争议的。
更新:
为了安全起见,是的,您的其他建议是正确的。 (您肯定不想为此使用Page_Load)。 MVC 提供
AuthorizationAttribute
< /a> 开箱即用。您可以将其应用于控制器类(或基类)和单个操作,并且可以指定授权角色。对于大多数场景来说都很好。如果您想做一些自定义的事情,您可以创建一个自定义属性。In theory it is, because a view inherrits from
ViewPage
, which inherrits the events fromPage
. However it will be theLoad
event (it probably wont be automatically wired toPage_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.