2014-10-03 21 views
3

我使用nopCommmerce 3.40,MVC 5MVC路線返回錯誤:兒童的行爲是不允許進行重定向操作

我必須做的插件,使路由一個路由的行動

但我得到錯誤的外觀像:

Error executing child request for handler'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. 

Child actions are not allowed to perform redirect actions 

我的代碼

[AdminAuthorize] 
    public ActionResult Configure() 
    { 

     return RedirectToRoute("PluginName.ActionName"); 
    } 

    public ActionResult ActionName() 
    { 
     // i want to call/ return this from Configure mathod 
    } 

RouteProvider

routes.MapRoute("PluginName.ActionName", "Admin/Plugins", 
        new { controller = "Controller", action = "Action" }, 
        new[] { "PluginName.ActionName.Controllers" } 
        ).DataTokens.Add("area", "admin"); 

回答

1

您收到的錯誤消息說明了一切。您無法在那裏執行重定向。實際上,插件配置操作是使用子操作調用的,因此通常使用[ChildActionOnly]屬性進行修飾。你可以在這個答案Why are Redirect Results not allowed in Child Actions in Asp.net MVC 2找到更詳細的消息。

您應該重構代碼以從其他位置調用重定向,例如,應用於主操作的主操作或自定義操作篩選器。

但是,由於調用您的Configure動作的代碼因爲屬於nopCommerce而不屬於您的控件,而不屬於您的插件,所以最好的辦法是動態注入自定義動作過濾器並在其中執行重定向。

相關問題