Copied to clipboard

Flag this post as spam?

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


  • nirmala 4 posts 74 karma points
    Mar 30, 2018 @ 11:04
    nirmala
    0

    Route hijacking with custom controller & return url with custom parameter

    Hi,

    I'm having a little trouble in returning url with custom parameter (something that I've done a few times so probably missing something obvious).

    i'm trying to return product detail page from surface controller to umbraco template with url parameter.

    i had followed the methods below

    https://24days.in/umbraco-cms/2014/urlprovider-and-contentfinder/

    https://stackoverflow.com/questions/18201997/url-rewriting-in-asp-net-umbraco?rq=1

    https://stackoverflow.com/questions/46158391/making-custom-urls-in-umbraco-7-6-without-document-type

    but it's not working .i don't know what i'm missing

    Thanks in advance.

    Regards,

    Nirmala

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Apr 02, 2018 @ 07:57
    Alex Skrypnyk
    0

    Hi Nirmala

    Can you show the code, please?

    Alex

  • nirmala 4 posts 74 karma points
    Apr 03, 2018 @ 04:54
    nirmala
    0

    Hi alex,

    Here is my controller

    namespace UmbracoinmvcTrial.Controllers
    {
        public class PropertySurfaceController : Umbraco.Web.Mvc.SurfaceController
        {
            private MyContext db = new MyContext();//I'm using AnotherDB to return Data
    
            //Umbraco- PropertyDetail template
            public ActionResult PropertyDetailTemplate(int? PropertyID)
            {
                Session["PropertyID"] = PropertyID;
                return View("PropertyDetail");
            }
    
            //PropertyDetail-partialview
            public ActionResult propertyDetail(int PropertyID)
            {
                PropertyDetailVM ProDatalList = new PropertyDetailVM();
                List<PropertyDataVM> prodetail = new List<PropertyDataVM>();
    
                //get propertydetail
                PropertyDetailRepo pd = new PropertyDetailRepo();
                prodetail = pd.Listdata(PropertyID);
                ProDatalList.PropertyDetails = prodetail;
    
                return PartialView("PropertyDetail", ProDatalList);
            }
        }
    
    }
    

    my RenderMvcController

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Models;
    
    namespace UmbracoinmvcTrial.Controllers
    {
        public class PropertyDetailController : Umbraco.Web.Mvc.RenderMvcController
        {
            // GET: PropertyDetail
            public ActionResult PropertyDetail(RenderModel model)
            {
                //Do some stuff here, the return the base Index method
    
                return base.Index(model);
            }
    
        }
    }
    

    my model

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using System.Web.Routing;
    using Umbraco.Core.Models;
    using Umbraco.Web.Models;
    
    namespace UmbracoinmvcTrial.Models.VMModel
    {
        public class PropertyDetail : RenderModel
        {
            public PropertyDetail(IPublishedContent content) : base(content) { }
    
            string myparameter { get; set; }
    
            public IList<PropertyDataVM> PropertyDetails { get; set; }
    
        }
    
    }
    

    my startup.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Publishing;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    using UmbracoinmvcTrial.Controllers;
    using UmbracoinmvcTrial.Models;
    
    namespace UmbracoinmvcTrial.App_Start
    {
    
    
        public class Startup : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                //With the url providers we can change node urls.
                UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, PropertyDetailUrlProvider>();
    
                //With the content finder we can match nodes to urls.
                ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, PropertyDetailContentFinder>();
            }
        }
    
        public class PropertyDetailUrlProvider : IUrlProvider
        {
            private MyContext db = new MyContext();
    
            public string GetUrl(Umbraco.Web.UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
            {
                var content = umbracoContext.ContentCache.GetById(id);
                if (content != null && content.DocumentTypeAlias == "PropertyDetail" && content.Parent != null)
                {
                    var proid = HttpContext.Current.Session["PropertyID"].ToString();
                    var property = db.Properties.Find(proid);
                    var shortname = property.ShortName;
                    if (shortname != null)
                    {
                        //This will add the selected date before the node name.
                        //For example /propertydetail/ becomes /propertydetail/shortname/.
                        var url = content.Parent.Url;
                        if (!(url.EndsWith("/")))
                        {
                            url += "/";
                        }
                        return url + "/" + shortname + "/";
                    }
                }
    
                return null;
            }
    
            public IEnumerable<string> GetOtherUrls(Umbraco.Web.UmbracoContext umbracoContext, int id, Uri current)
            {
                return Enumerable.Empty<string>();
            }
    
        }
    
        public class PropertyDetailContentFinder : IContentFinder
        {
            public bool TryFindContent(PublishedContentRequest contentRequest)
            {
                try
                {
                    if (contentRequest != null)
                    {
                        //Get the current url.
                        var url = contentRequest.Uri.AbsoluteUri;
    
                        //Get the propertydetail nodes that are already cached.
                        var cachedproNodes = (Dictionary<string, ContentFinderItem>)HttpContext.Current.Cache["CachedPropertyDetailNodes"];
                        if (cachedproNodes != null)
                        {
                            //Check if the current url already has a propertydetail item.
                            if (cachedproNodes.ContainsKey(url))
                            {
                                //If the current url already has a node use that so the rest of the code doesn't need to run again.
                                var contentFinderItem = cachedproNodes[url];
                                contentRequest.PublishedContent = contentFinderItem.Content;
                                contentRequest.TrySetTemplate(contentFinderItem.Template);
                                return true;
                            }
                        }
    
                        //Split the url segments.
                        var path = contentRequest.Uri.GetAbsolutePathDecoded();
                        var parts = path.Split(new[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
    
                        //The propertydetail items should contain 2 segments.
                        if (parts.Length == 2)
                        {
                            //Get all the root nodes.
                            var rootNodes = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetAtRoot();
    
                            //Find the propertydetail item that matches the last segment in the url.
                            var proItem = rootNodes.DescendantsOrSelf("PropertyDetail").Where(x => x.UrlName == parts.Last()).FirstOrDefault();
                            if (proItem != null)
                            {
                                //Get the propertydetail item template.
                                var template = ApplicationContext.Current.Services.FileService.GetTemplate(proItem.TemplateId);
    
                                if (template != null)
                                {
                                    //Store the fields in the ContentFinderItem-object.
                                    var contentFinderItem = new ContentFinderItem()
                                    {
                                        Template = template.Alias,
                                        Content = proItem
                                    };
    
                                    //If the correct node is found display that node.
                                    contentRequest.PublishedContent = contentFinderItem.Content;
                                    contentRequest.TrySetTemplate(contentFinderItem.Template);
    
                                    if (cachedproNodes != null)
                                    {
                                        //Add the new ContentFinderItem-object to the cache.
                                        cachedproNodes.Add(url, contentFinderItem);
                                    }
                                    else
                                    {
                                        //Create a new dictionary and store it in the cache.
                                        cachedproNodes = new Dictionary<string, ContentFinderItem>()
                                    {
                                        {
                                            url, contentFinderItem
                                        }
                                    };
                                        HttpContext.Current.Cache.Add("cachedproNodes",
                                                cachedproNodes,
                                                null,
                                                DateTime.Now.AddDays(1),
                                                System.Web.Caching.Cache.NoSlidingExpiration,
                                                System.Web.Caching.CacheItemPriority.High,
                                                null);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //Umbraco.LogException<PropertyDetailContentFinder>(ex);
                }
    
                return contentRequest.PublishedContent != null;
            }
    
    
            //ContentService.Published += Content_Published;
            private void Content_Published(IPublishingStrategy sender, PublishEventArgs<IContent> e)
            {
                //Clear the content finder cache.
                HttpContext.Current.Cache.Remove("CachedPropertyDetailNodes");
            }
        }
    }
    

    Regards

    Nirmala

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies