Umbraco沿着正常的mvc路线行进

时间:2017-03-10 23:07:52

标签: c# asp.net-mvc asp.net-mvc-5 umbraco umbraco7

我在FRESH mvc5项目中通过nuget安装了umbraco,我有一个普通的MVC控制器。

 public class HomeController : Controller
    {
        public ActionResult Dashboard(RenderModel model, int? Id)
        {              
            return Content("ok");
        }
}

问题是当我尝试进入/ Home / Dashboard

我收到此错误:No umbraco document matches the url '/home/dashboard'.

我怎么能让我的mvc控制器与我的umbraco共存。我对umbraco文件和模板路由不感兴趣只是让我的普通MVC控制器工作就好像什么都没发生一样。我打算以后获取内容我可以在我的mvc控制器中使用它。

2 个答案:

答案 0 :(得分:0)

Umbraco无法使用您必须使用SurfaceController继承的简单控制器。因为它适用于像这样的Surface Controller

public class AccountController : SurfaceController
{
        public ActionResult Dashboard(RenderModel model, int? Id)
        {              
            return Content("ok");
        }
}

您可以使用以下网址调用此方法 /一把umbraco /表面/ {controllername} / {行动} / {ID}

见文档: https://our.umbraco.org/documentation/reference/routing/surface-controllers

答案 1 :(得分:0)

您仍然可以将标准MVC控制器与Umbraco SurfaceController一起使用,只需记住Umbraco正在处理路由,因此您必须手动进行路由:

public class ApplicationEventHandler : IApplicationEventHandler
{
    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        RegisterCustomRoutes();
    }

    private static void RegisterCustomRoutes()
    {
        // this is just an example, modify to suit your controllers and actions
        System.Web.Routing.RouteTable.Routes.MapRoute(
           name: "WhateverController",
           url: "Whatever/{action}/{id}",
           defaults: new { controller = "Whatever", action = "DoTheThing", id = UrlParameter.Optional });
    }
}

然后WhateverController.DoTheThing(int id)应该可以在/ whatever / dothething / 1上访问,就像MVC通常那样。不幸的是,如果您有许多控制器和动作,则可能需要进行大量设置并需要维护,因此,您可能需要找到一种更简便的方法来批量生成这些路由。

相关问题