【问题标题】:MVC Routing in Umbraco instanceUmbraco 实例中的 MVC 路由
【发布时间】:2015-09-01 16:27:00
【问题描述】:

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

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

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

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

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

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

谢谢, 克雷格

【问题讨论】:

    标签: c# asp.net-mvc-4 umbraco7


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢伙计。原来我想多了,你的建议给我指明了正确的方向。我最终使用了@Html.ActionLink("Delete Objective", "removeObjective", "CPDPlanSurface", new { @planid = item.PlanID, @userName = Session["username"], @redirectID = 3660 }, null) 显然我对控制器中的 ActionResult 做了一些更改(更多参数)。干杯芽
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多