Skip to main content
 首页 » 编程设计

c#之Umbraco 实例中的 MVC 路由

2024年02月05日6findumars

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

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

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

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

最终我想以 www.mysite.com/mypage/removeObjective/5 的 URL 结束,但我不知道从哪里开始创建这条“路线”。

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

谢谢, 克雷格

请您参考如下方法:

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

@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(); 
    } 
} 

表面 Controller 的路径最终是这样的:

/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); 
    } 
} 

然后你应该能够像这样访问 Controller 上的方法:

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