2013-05-27 68 views
2

標準asp.NET中是否有與MVC.NET的OnActionExecuting等價的東西? ?OnActionExecuting在標準的asp.NET中的等價物?

我認爲這將是Page_Load因爲每次執行操作(或頁面加載)時都會調用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); 
     } 
+0

你有迄今爲止的代碼的例子嗎? –

+0

@RaimondKuipers我的asp.NET代碼或我正在轉換的MVC代碼? – anpatel

+0

都會有幫助 –

回答

2

首先,採取帳戶沒有操作是在ASP。因爲模型是不同的(基於事件) - 沒有方法(動作),你可以使用Action Filters來修飾,這些都是關於Page-Cycle事件。其次,在ASP.NET中,您可以使用HTTP modulesHttpApplication.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() 
    { 
    } 
} 
+0

所以我開始請求將類似於執行的動作?我認爲開始請求會在執行動作之前發生......在MVC頁面循環中,這就是爲什麼我認爲頁面加載可能更適合。實際上我非常困惑。那麼OnAuthorization會是什麼? – anpatel

+0

我猜它的AuthorizeRequest? – anpatel

+0

@MyName問題是你的目標是什麼,是指日誌?安全? –