是否有等价的MVC ?.NET的OnActionExecuting
在标准asp.NET??
我认为它将是Page_Load
,因为OnActionExecuting
将在每次执行动作(或页面加载)时被调用。但是当我尝试使用Page_Load代替时,我遇到了继承问题。
由于很难使我的解决方案与Page_Load
一起工作,我想我可能没有最好的…解决方案。
对于它们是否相等或足够接近有什么想法吗?
背景:我正在将mvc应用程序的一部分转换为标准的。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);
}
首先,考虑到没有 Actions在ASP中。因为模型是不同的(基于事件的)——没有方法(动作)可以用动作过滤器来修饰,它都是关于Page-Cycle事件的。
第二,在ASP中。. NET,您可以使用HTTP模块(特别是HttpApplication.BeginRequest
),以便通过添加所需的逻辑来拦截对应用程序页面的传入请求。
例如: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()
{
}
}