Umbraco实例中的MVC路由
本文关键字:MVC 路由 实例 Umbraco | 更新日期: 2023-09-27 18:10:47
我想知道是否有人能帮我。。。。
我在一个控制器(名为CPDPlanSurfaceController的控制器(中创建了一个非常基本的ActionResult
public ActionResult removeObjective(int planId)
{
return RedirectToCurrentUmbracoPage();
}
我想创建一个映射到这个ActionResult的URL(很明显,除了这个重定向之外,还有更多(。我不能使用@Url.Action文本,因为这在Umbraco中似乎不起作用(Url总是空的(。另一个问题似乎是在我的app_start文件夹中没有routeconfig.cs。所以我真的不知道从哪里开始。
最终,我想得到一个网址www.mysite.com/mypage/removeObjective/5,但我甚至不知道从哪里开始创建这个"路线"。
有谁能抽出五分钟时间给我指一指正确的方向吗。
谢谢,Craig
希望这能让你开始。我这里可能有几个错误,但应该很接近。我通常能做
@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")