1
votes

I have looked all around at Umbraco route hijacking and using IContentFinder and IUrlProvider and I am still a little lost on how to do what I want.

I have a controller that matches a document type which I have a base page for in Umbraco.

I made a document type called "Ads" and a controller called "AdsController: RenderMvcController"

I have a basic method to catch all like this

public ActionResult Index(RenderModel model)

It works if the route is like this

http://www.example.com/Ads?parent=Cars&child=American

but I want it to hit if I do this :

http://www.example.com/Ads/Cars/American

So basically nice MVC routing. I think content provider only lets you find content that is in your actual tree and this content isn't. I am going to find it using C# in the DB and render a custom view and return it using a custom view model.

I know how to do the view part. Just need to know how to hijack all routes under a parent route so my base controller method hits without having to create a bunch of templates or action methods for each AD category type.

2

2 Answers

0
votes

When your app is starting, overwrite the ApplicationStarted method, and then add your custom routes. Like this:

public class ApplicationStartup : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            //Custom route
            RouteTable.Routes.MapRoute(
                "Ads",
                "Ads/{action}/{id}",
                new
                {
                    controller = "Ads",
                    action = "Index",
                    id = UrlParameter.Optional
                });
        }
    }

Remember to add using Umbraco.Core;

3
votes

Now no longer in beta you can do the following code which will give you a full model that includes the umbraco data for that page:

Route:

    public class ApplicationStartup : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
RouteTable.Routes.MapUmbracoRoute("routename", "Ads/{parent}/{child}", new { controller = "AdsController", action = "Index" }, new PublishedPageRouteHandler(PublishedPageId));
}

Handler

public class PublishedPageRouteHandler :  UmbracoVirtualNodeRouteHandler
{
    private readonly int _pageId;

    public PublishedPageRouteHandler(int pageId)
    {
        _pageId = pageId;
    }

    protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
    {
        var helper = new UmbracoHelper(umbracoContext);
        return helper.TypedContent(_pageId);
    }
}

Controller :

public class AdsController : RenderMvcController
{
    public override ActionResult Index(AdModel model)
    {
       return base.Index(model);
    }
}

Model:

  public class AdModel : RenderModel
    {
        public string Parent{ get; set; }
        public string Child { get; set; }

        public AdModel () : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
        {
        }
    }

View:

@inherits UmbracoViewPage<AdModel>

<h1>@Model.Parent</h1>