Copied to clipboard

Flag this post as spam?

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


  • Hein 29 posts 158 karma points
    Sep 12, 2017 @ 08:17
    Hein
    0

    Making custom URLs in Umbraco 7.6 without document type

    What I have

    I'm making a site using Umbraco 7.6.6 and I want to access a third party database of my client. For that I've made a custom controller named QuestionsController. Inside this I've created an action:

    public class QuestionsController : SurfaceController
    {
        private QuestionService _questionService = new QuestionService();
    
        [HttpGet]
        public ActionResult Index()
        {
            return PartialView("~/Views/MacroPartials/Questions.cshtml", _questionService.ReadFile());
        }
    }
    

    This page index page works fine and is called by this code on the view of my document type:

    Html.RenderAction("index", "Questions");
    

    Overview page (just an image):

    Here is my model I've created

    public class Question
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public User User { get; set; }
        public DateTime Created { get; set; }
        public List<Comment> Comments { get; set; }
    }
    

    What I will

    Now I'll to create a details page with more information on the front side.

    [HttpGet]
    [Route("~/questions/details/{id}")]
    public ActionResult Details(int id)
    {
        return View(_questionService.ReadFile().ElementAt(id));
    }
    

    Redirecting to that page I'll do like this:

    <a href="~/questions/details/@q.ID">@q.Title</a>
    

    But this doesn't work at all. The detail page gives me a 404 (page not found) error.

    Question

    My question is now: How could I create custom URLs like ~/questions/details/{id} without a document type?

  • Marcio Goularte 374 posts 1346 karma points
    Sep 12, 2017 @ 11:57
    Marcio Goularte
    1

    Hi Hein,

    You need to create an Application startup events where you configure the MapHttpAttributeRoutes.

    public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                config.MapHttpAttributeRoutes();               
    
            }
        }
    
    using System;
    using System.Web.Http;
    using System.Web.Routing;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    
    namespace OurUmbraco.MyNamespace.Api
    {
        public class MapRoutesEvent : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbraco, ApplicationContext context)
            {    
    
                WebApiConfig.Register(GlobalConfiguration.Configuration);
    
            }
        }
    }
    

    UPDATE: This setting will only work for WebAPI. I think you need to do this:

    https://shazwazza.com/post/custom-mvc-routing-in-umbraco

    https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/59312-Umbraco-7-setup-with-custom-route-values

    http://jamessouth.me/archive/fun-with-umbracovirtualnoderoutehandler/

Please Sign in or register to post replies

Write your reply to:

Draft