Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ayo Adesina 430 posts 1023 karma points
    Jan 03, 2015 @ 19:32
    Ayo Adesina
    0

    Umbraco 7 MVC custom Routes

    A web-application with complex business logic. but I want to use umbraco as the cms.

    I also want to have a traditional Model View Controller set up.

    I want to have custom ViewModels attached to strongly typed razor views. I want Controllers to process request and return my custom viewmodels.

    So far I have done:

    A Web Project with Umbaco7 installed.

    1. Created a StartupHandler : IApplicationEventHandler class that difines routes in the OnApplicationStarted method

    2. A class that implements IApplicationEventHandle in the OnApplicationStarted method - I am defining my Routes - So far just this one:

      public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
      {
          //Create a custom routes
          RouteTable.Routes.MapRoute(
              "",
              "Menu",
              new
              {
                  controller = "Menu",
                  action = "Index"
              });
      
      
      }
      
    3. I have matching Controller:

      public class MenuController : PluginController
      {
      MenuService menuService;
      
      
      public MenuController() : this(UmbracoContext.Current)
      {
          menuService = new MenuService();
      }
      
      
      public MenuController(UmbracoContext umbracoContext): base(umbracoContext){}
      
      
      public ActionResult Index()
      {
          var model = new MainMenuViewModel();
          model = menuService.GetMenu();
          return View("Menu", model);
      }
      }
      
    4. I have a view - Menu.cshtml with the first line

      @inherits Umbraco.Web.Mvc.UmbracoViewPage <AsoRock.Models.ViewModels.MainMenuViewModel>
      @{
          Layout = "~/Views/SiteLayout.cshtml";
        }
      
      
      
      &lt;div&gt;webpage with using the custom model&lt;/div&gt;
       &lt;div&gt;Test&lt;/div&gt;
      
    5. My custom view model inheriets RenderModel..

       public class MainMenuViewModel : RenderModel
       {
          public List<Category> Categories { get; set; }
      
      
      
      public MainMenuViewModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) {
      
      
      }
      
      
      public MainMenuViewModel(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
      public MainMenuViewModel(IPublishedContent content) : base(content) { }
      
      }

    Now when I go to website.com/menu the controller is hit, which what I want, but when it trys to return the view,

    I get an error inside the MainMenuViewModel - On this line

     public MainMenuViewModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) {
    

    pageId is NULL...... any one know why?

    Could some one please advice me on my set up, and could someone shed any light on how the custom routes are related to the umbraco content tree? This approach seems to by pass umbraco completely. Do I need to create a node in umbraco called menu...??

    Very confused! Help please :-\

  • Ayo Adesina 430 posts 1023 karma points
    Jan 03, 2015 @ 19:34
    Ayo Adesina
    0

    and whats the Different between Surface Controller VS Plugin Controller Vs Umbraco Controller. Which one do you inherit from? when? Why? How?

  • Tim Anderson 20 posts 83 karma points
    Jan 05, 2015 @ 17:23
    Tim Anderson
    1

    I would recommend reading these blog posts from Shannon Deminick, this should help answer some of your questions and get you up and running, from there you should be able to figure things out.

    The blogs make reference to v6.1.6 and above, but this should translate over to the latest v7 builds.

    http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco

    http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/

    A word of warning though, what you are effectively doing is called route hijacking, and using custom MVC routes like you are doing skips certain parts of the Umbraco Request Pipeline (Hence why your pageId is NULL), the blog posts above provide a solution to overcome this issue, but the solution is by no means perfect. Having used this approach myself I have discovered that there are certain compromises you will have to live with if you choose to go down this route.

    • Integration with the content tree is not trivial, as I am assuming your content is coming from a separate data source, and is therefore not Umbraco content?
    • Umbraco Surface Controllers which use a postback will not function correctly on a hijacked route when you use a custom MVC route like above.

    Hopefully that leads you on the correct path.

Please Sign in or register to post replies

Write your reply to:

Draft