Copied to clipboard

Flag this post as spam?

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


  • Mitch Stewart 1 post 71 karma points
    Jul 20, 2016 @ 16:46
    Mitch Stewart
    0

    Creating MVC Style Views Using Umbraco Templates

    I am currently converting an MVC web page to Umbraco. The web page has various controllers and views:

    Controllers
         Services
    Views
         Services
              Index
              CustomSoftwareDevelopment
    

    I need to be able to render these views as such:

    localhost:8080/Services/ (*Redirects to Index) localhost:8080/Services/CustomSoftwareDevelopment

    I also need to be able to use the Document Types and Templates for my views.

    How can I accomplish this?

    Here's what I've tried:

    1. Simply renaming the content page to Services/CustomSoftwareDevelopment doesn't work as Umbraco renames the page to servicescustomsoftwaredevelopment.
    2. Route Hijacking (https://our.umbraco.org/documentation/reference/routing/custom-controllers) But I am unable to render views created in Umbraco. I can render Views in my Views folder, users also need to be able to edit these views as Templates. Is there a way I can render views created in Umbraco by referencing some Umbraco HTML helper? (i.e. @Umbraco.RenderView("CustomSoftwareDevelopment")?
    3. Routing via Template - This would work, but I lose the template - document type relationship. If I have one document type and five templates, is the one document type supposed to a conglomeration of the possible properties the five templates could have?
  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Jul 22, 2016 @ 22:36
    Marc Goodson
    0

    Hi Mitch

    I know your converting an MVC web page to Umbraco, but does this page contain functionality, or does it just display content ?

    If they are just content pages, and you don't need any controller logic to run, then you can create a content item called 'Services' at the root of your site, that will have a 'Services' template, that contains your index html.

    Then underneath this Services page, you can create your content pages, eg CustomSoftwareDevelopment, and the url for this will then be /Services/CustomSoftwareDevelopment ...

    If you need to run the request through your Services controller to perform some kind of logic, combined with Umbraco content then you could create a custom route: https://our.umbraco.org/documentation/reference/routing/custom-routes There is a technique described here to create a virtual node route handler: http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/ that would enable you to fire your custom logic and also pull back content from Umbraco to display your pages.

    In terms of what you've tried:

    1) There is a little know magic umbraco property called umbracoUrlAlias - if you add this to your content document type, it will enable you to specify alternative urls for your content item: (including folders) https://our.umbraco.org/wiki/reference/umbraco-best-practices/umbracourlalias/

    2) When route hijacking there is a helper

    return CurrentTemplate(model);

    that will return the view that the current page has been published with, and you can use ?altTemplate=alternativetemplatealias on the querysting (or the cleaner /myfolder/mypage/alternativetemplatealias) to force the page to be displayed with a different template.

    or you can return other Umbraco templates by specifying the path to the razor file eg

    return View("~/views/myotherumbracotemplate.cshtml",model)

    I don't know if that random blast of info helps you in the right direction ?

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft