是否有 MVC.NET 的等效项OnActionExecuting
在标准 asp.net 中? ?
我以为会是Page_Load
since OnActionExecuting
每次执行操作(或加载页面)时都会调用。但当我尝试使用 Page_Load 时,我遇到了继承问题。
因为我的解决方案很难与Page_Load
我想我可能没有最好的解决方案。
关于它们是否等效或足够接近有什么想法吗?
背景:
我正在将 MVC3 应用程序的一部分转换为标准 .NET 以包装在 SharePoint Web 部件中。
这是我正在尝试翻译的 MVC 代码,您可以看到我正在翻译的用户安全位:
protected override void OnActionExecuting(ActionExecutingContext filterContext) {
if (!SiteCacheProvider.ItemCached(enmCacheKey.SiteSetting)) {
if (filterContext.IsImplementedGeneralPrincipal()) {
IUserProfile userProfile = ((IGeneralPrincipal)filterContext.HttpContext.User).UserProfile;
SiteCacheProvider.ChangeSiteSetting(userProfile.SiteID);
}
}
base.OnActionExecuting(filterContext);
}
首先,考虑到no操作位于 ASP.NET 中,因为模型不同(基于事件) - 没有可以用来装饰的方法(操作)动作过滤器,这都是关于页面循环 events.
其次,在 ASP.NET 中,您可以使用HTTP 模块 (HttpApplication.BeginRequest特别是),以便通过添加所需的逻辑来拦截对应用程序页面的传入请求。
来自 MSDN:
HTTP模块用于拦截HTTP请求以进行修改或利用
根据身份验证等需求基于 HTTP 的请求,
授权、会话/状态管理、日志记录、修改响应、
URL重写、错误处理、缓存......
例如:
using System;
using System.Web;
using System.Collections;
public class HelloWorldModule : IHttpModule
{
public string ModuleName
{
get { return "HelloWorldModule"; }
}
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<h1>HelloWorldModule: Beginning of Request</h1><hr>");
}
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<hr><h1>HelloWorldModule: End of Request</h1>");
}
public void Dispose()
{
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)