Umbraco 实例中的 MVC 路由

MVC Routing in Umbraco instance

我想知道是否有人可以帮助我....

我在控制器中创建了一个非常基本的 ActionResult(控制器称为 CPDPlanSurfaceController)

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

并且我想创建一个映射到此 ActionResult 的 URL(显然,除了此重定向之外,还有更多内容)。我不能使用 @Url.Action 文本,因为这在 Umbraco 中似乎不起作用(url 始终为空)。另一个问题似乎是我的 app_start 文件夹中没有 routeconfig.cs。所以我真的不知道从哪里开始。

最终我想得到 www.mysite 的 URL。com/mypage/removeObjective/5 但我不知道从哪里开始创建这个 'route'.

谁能给我五分钟的时间来指出正确的方向。

谢谢, 克雷格

希望这能让您入门。我这里可能有几个错误,但应该很接近。我通常可以做到

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

@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

我相信如果你想做你自己的自定义路由,你需要做这样的事情:

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")