2016-02-19 69 views
0

該標題有誤導性,但我不確定如何更好地說出它。我的BaseController可以有一個方法返回一個重定向或true?

我的控制器全部繼承自BaseController。我想在BaseController中有一個方法,我可以從各種操作中調用。我想是這樣的:

public virtual object CheckValues(Guid value1, string value2) 
{ 
    if (value2 == const_SomeValue || value1 == GetCurrentId()) 
    { 
     return true; 
    } 
    return RedirectToAction("index"); 
} 

基本上,我想壽有將檢查某些事情的方法,如果失敗,做一個重定向。我的控制器行動將檢查它是這樣的:

public virtual ActionResult overview(Guid? id) 
{ 
    CheckValues(id, string.Empty); // on fail this redirects 

    // Continue with this Action 
    return View(); 
} 

我的許多控制器行動將利用CheckValues方法。

有沒有一個好的或正確的方法來做到這一點?

更新:我想分享我的解決方案。我喜歡它是如何出來的。

我現在控制器可以是這樣的:

[CheckId()] // I can overload the name of the Id, the redirect Action and/or contoller 
public virtual ActionResult overview(Guid? id) 
{ 
    //... Logic for my action 
    return View(); 
} 

我的過濾器看起來是這樣的:

public class CheckIdAttribute : ActionFilterAttribute 
{ 
    public string IdValue { get; set; } 
    public string RedirectAction { get; set; } 
    public string RedirectController { get; set; } 

    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     // I wanted to be able to override the redirect and 
     // the name of the id to check if necessary. Or just 
     // use defaults. 
     if (string.IsNullOrEmpty(IdValue)) 
      IdValue = "id"; 

     if (string.IsNullOrEmpty(RedirectAction)) 
      RedirectAction = "index"; 

     if (string.IsNullOrEmpty(RedirectController)) 
      RedirectController = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; 

     var isValue1Valid = filterContext.ActionParameters.ContainsKey(IdValue) && 
      (filterContext.ActionParameters[IdValue] != null && (Guid)filterContext.ActionParameters[IdValue] != Guid.Empty); 

     if (!isValue1Valid) 
     { 
      filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { action = RedirectAction, controller = RedirectController })); 
     } 
    } 
} 
+1

可能有一個操作返回一個BaseController中的操作,該操作將用戶發送到與您正在使用的控制器相關的視圖。但是將RedirectToAction作爲對象返回是討厭的;爲什麼不在基地做兩件事。一個返回一個布爾值,並根據跳轉到您的控制器重定向的結果。 – MartijnK

+0

嗯...好主意。我不喜歡我的想法。我喜歡乾淨和基於模式。 –

+1

或者,將bool返回函數移到別的地方(不同的類或服務,遠離控制器),只留下BaseController中的Action。這將進一步清理您的控制器並保持整潔。 :) – MartijnK

回答

3

基類方法的替代方法是行動的過濾器。你的控制器動作看起來是這樣的:

[CheckValues(Value1 = "id", Value2 = "")] 
public ActionResult overview(Guid? id) 
{ 
    // Continue with this Action 
    return View(); 
} 

然後在操作過濾器,覆蓋OnActionExecuting檢查參數,並可能重定向。

public class CheckValuesAttribute : ActionFilterAttribute 
{ 
    public string Value1 { get; set; } 
    public string Value2 { get; set; } 

    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
    var isValue2Valid = filterContext.ActionParameters.ContainsKey(Value2) && 
         filterContext.ActionParameters[Value2] == const_SomeValue; 
    var isValue1Valid = filterContext.ActionParameters.ContainsKey(Value1) && 
         filterContext.ActionParameters[Value1] == GetCurrentId(); 

    if (!isValue1Valid || !isValue2Valid) 
     filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { action = "Index"})); 
    } 
} 

以上仍然需要一些調整來應對時Value2缺失/空字符串和鑄造Value1爲GUID的情況,但這是它的要點。您設置filterContext.Result的行可能會使您的操作短路,從而實際上從未執行 - 重定向會在請求發生之前發生,然後再發送到您的控制器操作。

+0

我知道我曾見過類似的東西,但不記得名字。謝謝。研究。 –

+0

乾淨整潔的解決方案。 – MartijnK

+0

還有一個問題。有沒有辦法在過濾器中獲得Request.Url.AbsolutePath? –

相關問題