Copied to clipboard

Flag this post as spam?

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


  • TomD 11 posts 103 karma points
    Jan 06, 2016 @ 13:45
    TomD
    0

    Register route to profile of an Umbraco Member

    Hey all,

    I have to route to the profile of an Umbraco Member in the frontend of my website. The problem is that I have no idea how to do that.

    I tried to register a route in an Umbraco Startup file like localhost/user/username and set the belonging controller and the action method as parameters.

    Up to this point everything works fine, but I don’t know how to render this in an Umbraco template. I need access to my Master to make work JavaScript or embed my CSS files.

    Since I’m relatively new to Umbraco and MVC, I hope that somebody can help me with the following code:

    This is my Startup file:

    public static void RegisterRoutes(RouteCollection routes)
    {
            routes.MapRoute(
                "ProfileView",                                                                          // Route name
                "user/{profileURLtoCheck}",                                                             // URL with parameters
                new { controller = "ViewProfileSurface", action = "ViewMemberProfile" }                 // Parameter defaults
            );
    
    }
    

    My ActionResult from my Surfacecontroller ViewProfileSurfaceController:

        [Authorize]
        [ActionName("ViewMemberProfile")]
        public ActionResult ViewMemberProfile(string profileURLtoCheck)
        {
            var membershipService = ApplicationContext.Current.Services.MemberService;
    
            IMember findMember = membershipService.GetMembersByPropertyValue("profileURL", profileURLtoCheck).SingleOrDefault();
    
            //Create a view model
            ViewProfileViewModel profile = new ViewProfileViewModel();
    
            //Check if we found member
            if (findMember != null)
            {
    
                profile.MemberID = findMember.Id;
                profile.Email = findMember.Email;
    
                profile.Firstname = findMember.Properties["firstname"].Value.ToString();
                profile.Lastname = findMember.Properties["lastname"].Value.ToString();
                profile.Nickname = findMember.Properties["nickname"].Value.ToString();
                profile.Age = Convert.ToInt32(findMember.Properties["age"].Value.ToString());
                profile.ZipCode = Convert.ToInt32(findMember.Properties["zipCode"].Value.ToString());
    
                profile.LastLoginDate = findMember.LastLoginDate;
                profile.LastLoginDate = DateTime.Parse(findMember.Properties["umbracoMemberLastLogin"].Value.ToString());
                profile.RegistredFrom = DateTime.Parse(findMember.Properties["registeredFrom"].Value.ToString());
    
                profile.Place = findMember.Properties["place"].Value.ToString();
                profile.Birthday = DateTime.Parse(findMember.Properties["birthday"].Value.ToString());
                profile.Sex = umbraco.library.GetPreValueAsString(Convert.ToInt32(findMember.Properties["sex"].Value.ToString()));
                profile.Sign = umbraco.library.GetPreValueAsString(Convert.ToInt32(findMember.Properties["sign"].Value.ToString()));
                profile.Description = findMember.Properties["description"].Value.ToString();
                profile.Company = findMember.Properties["company"].Value.ToString();
                profile.Website = findMember.Properties["website"].Value.ToString();
    
            }
            else
            {
                //Couldn't find the member return a 404
                return new HttpNotFoundResult("The member profile does not exist");
            }
    
            return PartialView("ViewProfile", profile);
    
        }
    

    My PartialView:

    @model Umbraco714.Models.ViewProfileViewModel
    
    @using System;
    @using System.Collections.Generic;
    @using System.Linq;
    @using System.Web;
    @using Umbraco714.Models;
    @using Umbraco.Core;
    @using Umbraco.Core.Persistence;
    @using Umbraco.Core.Services;
    @using Umbraco.Web.Security;
    @using Umbraco.Web;
    @using Umbraco.Web.Models;
    @using Umbraco.Core.Models;
    @using Umbraco714.Repositories;
    
    <h2>@Model.Firstname @Model.Lastname</h2>
    <img src="~/media/@member.Id/@media.Id/@name" alt="@Model.Nickname" />
    
    
    <h3>Vorname: @Model.Firstname</h3>
    <h3>Nachname: @Model.Lastname</h3>
    
    <h3>Benutzername: @Model.Nickname</h3>
    
    <h3>Alter: @Model.Age</h3>
    <h3>Geburtstag: @Html.DisplayFor(m => m.Birthday)</h3>
    
    <h3>Wohnort: @Model.ZipCode @Model.Place</h3>
    
    <h3>Geschlecht: @Model.Sex</h3>
    <h3>Sternzeichen: @Model.Sign</h3>
    
    <h3>Firma: @Model.Company</h3>
    <h3>Webseite: @Model.Website</h3>
    
    <h3>Beschreibung: @Model.Description</h3>
    
    <h3>Letzer Login: @Html.DisplayFor(m => m.LastLoginDate)</h3>
    <h3>Registriert seit: @Html.DisplayFor(m => m.RegistredFrom)</h3>
    
    @Html.HiddenFor(model => model.MemberID)
    

    My Question is now, how to get access to my Master or to a template to use the Layout of the Master file?

    thanks,

    Tom

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 10x admin c-trib
    Jan 08, 2016 @ 07:42
    Dave Woestenborghs
    0

    Hi Tom,

    Maybe this article on displaying members from Warren Buckley can help you out :

    http://creativewebspecialist.co.uk/2013/12/03/using-umbraco-pipeline-for-member-profile-urls/

    Dave

  • TomD 11 posts 103 karma points
    Jan 08, 2016 @ 15:12
    TomD
    1

    Hi Dave,

    Thanks for your reply. I have already found this article previously, but Warren describe another solution than mine. Unlike Warren, I’m using a Controller and a custom route.

    Nevertheless, I tried to rewrite his example for Umbraco 7 and used Umbraco.Core.Models.Member since BusinessLogic is obsolete. But without success. Perhaps you could tell me what I have done wrong?

    When I try to navigate to a profile of a member with a path like this: localhost/member/tomd I always get the error message: Page not found. No umbraco document matches the url '/member/tomd.

    Here is my new code:

    My iContentFinder:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Persistence.Querying;
    using Umbraco.Web.Routing;
    
    namespace Umbraco714.Utils
    {
        public class MemberProfileContentFinder : IContentFinder
        {
    
            public bool TryFindContent(PublishedContentRequest contentRequest)
            {
                var urlParts = contentRequest.Uri.GetAbsolutePathDecoded().Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
    
                //Check if the Url Parts
                // Starts with /member/*
                if (urlParts.Length > 1 && urlParts[0].ToLower() == "member")
                {
                    //Lets try & find the member
                    var memberName = urlParts[1];
    
                    //Try and find a member where the property matches the memberName
                    List<IMember> tryFindMember = ApplicationContext.Current.Services.MemberService.GetMembersByPropertyValue("profileurl", memberName).ToList();
    
                    if (!tryFindMember.Any()) // try a partial match just in case
                    {
                        tryFindMember = ApplicationContext.Current.Services.MemberService.GetMembersByPropertyValue("profileurl", memberName, StringPropertyMatchType.Contains).ToList();
                    }
    
                    //See if tryFindMember is not null
                    if (tryFindMember.Any())
                    {
                        //Need to set the member ID or pass member object to published content
                        HttpContext.Current.Items["memberProfile"] = tryFindMember.FirstOrDefault();
    
                        //Add in string to items - for profile name user was looking for
                        HttpContext.Current.Items["memberName"] = memberName;
    
                        //Set the Published Content Node to be the /Profile node - can get properties off it & my member profile in the view
                        contentRequest.PublishedContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute("/Test2View");
                    }
    
                    //Return true to say found something & stop pipeline & other contentFinder's from running
                    return true;
                }
    
                //Not found any content node to display/match - so run next ContentFinder in Pipeline
                return false;
            }
    
        }
    }
    

    My Umbraco Startup:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Publishing;
    using Umbraco.Core.Services;
    using Umbraco.Core.Persistence;
    using Umbraco.Web.Routing;
    
    namespace Umbraco714.Utils
    {
        public class UmbracoStartup : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                // Insert my MemberProfileContentFinder before ContentFinderByNiceUrl
                ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNiceUrl, MemberProfileContentFinder>();
            }
    
        }
    }
    

    My Umbraco Template named Test2View

    @using Member = Umbraco.Core.Models.Member
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @using Umbraco.Core.Models;
    
    @{
        Layout = "Master.cshtml";
    
        //Get the values out of the HTTPContext that we set in the iContentFinder
        var memberProfile   = (Member)HttpContext.Current.Items["memberProfile"];
        var memberName      = (string)HttpContext.Current.Items["memberName"];
    }
    
    
    <div class="container">
        <div class="col-md-12">
    
                @Html.Partial("TestProfileView", memberProfile);
    
        </div>
    </div>
    

    My Partialview named TestProfileView

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco.Core.Models.Member>
    <h2>Member Profile Partial</h2>
    
    <h3>Name: @Model.Name</h3>
    <h3>Email: @Model.Email</h3>
    

    Tom

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 10x admin c-trib
    Jan 09, 2016 @ 08:57
    Dave Woestenborghs
    0

    Hi Tom,

    Is Test2View a page in your website ?

    Dave

  • TomD 11 posts 103 karma points
    Jan 09, 2016 @ 09:56
    TomD
    0

    Yes, I created the template Test2View as a test in the Umbraco backend to show the profile of a member.

    Tom

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 10x admin c-trib
    Jan 09, 2016 @ 10:16
    Dave Woestenborghs
    101

    You also need to have a real content node.

    1. Create a doctype called memberprofile
    2. Create a page based on this doctype.
    3. Change your code to find the content node of doctype memberprofile

    UmbracoContext.ContentCache.GetByXPath("//YourMemberProfileDocTypeAlias").FirstOrDefault();

    Dave

  • TomD 11 posts 103 karma points
    Jan 09, 2016 @ 10:47
    TomD
    1

    Thanks for your help Dave! Now everything works fine. But I have one more question. Is there any possibility to use a MVC Controller on a part of this View anyway?

    Tom

  • 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