2015-09-01 38 views
0

我不知道,如果有人可以幫助我,請....MVC路由在一把umbraco實例

我在一個控制器(Controller稱爲CPDPlanSurfaceController)

public ActionResult removeObjective(int planId) 
    { 
     return RedirectToCurrentUmbracoPage(); 
    } 

和我創建了一個非常基本的ActionResult d喜歡創建一個映射到這個ActionResult的URL(顯然這裏會比這個重定向更多)。我不能使用@ Url.Action文本,因爲這似乎不適用於Umbraco(網址總是空的)。另一個問題似乎是我的app_start文件夾中沒有routeconfig.cs。所以我真的不知道從哪裏開始。

最終,我想結束一個www.mysite.com/mypage/removeObjective/5的URL,但我不知道哪裏可以開始創建這個'路線'。

任何人都可以讓我五分鐘指向正確的方向。

感謝, 克雷格

回答

3

希望這將讓你開始。我可能在這裏有幾個錯誤,但它應該很接近。我通常能夠做到

@Html.Action("removeObjective", "CPDPlanSurface", new RouteValueDictionary{ {"planId", 123} }) 

OR

@Html.ActionLink("Click Me!", "removeObjective", "CPDPlanSurface", new RouteValueDictionary{ {"planId", 123} }) 

我SurfaceController通常是這樣的:

using Umbraco.Web.Mvc; 
public class CPDPlanSurfaceController : SurfaceController 
{ 
    [HttpGet] 
    public ActionResult removeObjective(int planId) 
    { 
     return RedirectToCurrentUmbracoPage(); 
    } 
} 

到表面控制器的路徑最終被類似:

/umbraco/Surface/CPDPlanSurface/removeObjective?planId=123 

I相信如果你想要做自己的自定義路由,你需要做這樣的事情:

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.MapRoute(
      name: "CPDPlanRoutes", 
      url: "mypage/{action}/{planId}", 
      defaults: new { controller = "CPDPlanSurface", action = "Index", planId = UrlParameter.Optional }); 
    } 
} 

,然後ApplicationStarted:

public class StartUpHandlers : ApplicationEventHandler 
{ 
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) 
    { 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
    } 
} 

那麼你應該能夠得到的方法上你的控制器是這樣的:

@Url.Action("removeObjective", "CPDPlanSurface") 
+0

非常感謝matey。事實證明,我過度思考,你的建議指向了正確的方向。我結束了使用 '@ Html.ActionLink(「Delete Objective」,「removeObjective」,「CPDPlanSurface」,new {@planid = item.PlanID,@userName = Session [「username」],@redirectID = 3660}, null)' 很明顯,我對控制器中的ActionResult做了一些更改(更多參數)。 乾杯芽 – SxChoc