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);
}
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>
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?
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:
My ActionResult from my Surfacecontroller ViewProfileSurfaceController:
My PartialView:
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
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
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:
My Umbraco Startup:
My Umbraco Template named Test2View
My Partialview named TestProfileView
Tom
Hi Tom,
Is Test2View a page in your website ?
Dave
Yes, I created the template Test2View as a test in the Umbraco backend to show the profile of a member.
Tom
You also need to have a real content node.
UmbracoContext.ContentCache.GetByXPath("//YourMemberProfileDocTypeAlias").FirstOrDefault();
Dave
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
is working on a reply...