2014-01-14 113 views
0

我對Asp.net MVC非常陌生我在做過一些Webform之前完成了一些工作,但在這裏和我有不同之處如何在asp.net MVC應用程序中實現master_page Page_Load事件,我的代碼就像這樣:將asp.net webform轉換爲mvc

protected void Page_Load(object sender, EventArgs e) 
{ 
    int intResult = 0; 
    if (intResult != 0) 
    {  
     Response.Redirect("url"); 
    }    
} 

我想我在一個特定的控制器的所有動作都要經過這個if else我想有些事情比把if else在我所有的行動更有效。

回答

2

爲您的控制器創建一個actionfilter。

Creating an action filter and How to prevent an action from executing from a filter

//更新

在您的意見之一,我看到你想將用戶重定向到登錄頁面,當他不登錄有已經在ASP.NET MVC的東西是:該Authorize attribute

[Authorize] 
public class MyController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 
+0

你能解釋這個授權是如何工作的,因爲我的用戶存儲在數據庫中。 – danarj

+0

這沒問題。 ASP.NET(WebForms和MVC)都有一個用於認證/授權的成員模型。默認情況下,您應該使用此模型,並且此模型將底層存儲介質抽象出來。通過簡單地檢查用戶是否被認證,'Authorize'屬性也可以插入到這個模型中。另見http://weblogs.asp.net/scottgu/archive/2006/02/24/ASP.NET-2.0-Membership_2C00_-Roles_2C00_-Forms-Authentication_2C00_-and-Security-Resources-.aspx –