Voici un exemple complet d'utilisation du cache serveur ainsi que la récupération de l'url convivial d'une page.
Cache manager:
    public sealed class CacheManager
    {
        private static readonly CacheManager obj = new CacheManager();
        public CacheManager()
        {
        }
        public static CacheManager Instance
        {
            get
            {
                return obj;
            }
        }
        public void Add(string key,
            object data,
            CacheDependency dependencies,
            DateTime absoluteExpiration,
            TimeSpan slidingExpiration)
        {
            HttpContext.Current.Cache.Insert(key, data, dependencies, absoluteExpiration, slidingExpiration);
        }
        public object Get(string key)        {            if (HttpContext.Current.Cache[key] != null)                return HttpContext.Current.Cache[key];
            return null;        }   public class TermManager
   {
public static Dictionary<string,string> GetPagesUrls()
        {
            //list for saving the urls
            Dictionary<string, string> retVal = new Dictionary<string, string>();
            //current web
            SPWeb web = SPContext.Current.Web;
            //check if the current web is a publishing web
            if (PublishingWeb.IsPublishingWeb(web))
            {
                //get the pages list id
                Guid listId = PublishingWeb.GetPagesListId(web);
                //retrieve the pages list
                SPList pagesList = web.Lists[listId];
                //iterate trough the pages
                foreach (SPListItem item in pagesList.Items)
                {
                    //retrieve the terms used for the navigation (this can be multiple terms)
                    IList<NavigationTerm> terms = TaxonomyNavigation.GetFriendlyUrlsForListItem(item, false);
                    //check if the pages has terms associated with it
                    if (terms.Count > 0)
                    {
                        //use the GetResolvedDisplayUrl to retrieve the page friendly urls
                        retVal.Add(item.File.Url,terms[0].GetResolvedDisplayUrl(string.Empty));
                    }
                    else
                    {
                        //if the page does not have any terms get the normal url
                        retVal.Add(item.File.Url, item.File.Url);
                    }
                }
            }
            return retVal;
        }
        /// <summary>
        /// Retourne l'url convivial pour une page donnée
        /// </summary>
        /// <param name="navTermSet_cache_key">Clé de cache ex:  Navigation_Site_1234</param>
        /// <param name="pageUrl"> Page/article2.aspx</param>
        /// <returns> /monarticle</returns>
        public static string ResolveConvivialURL(string navTermSet_cache_key, string pageUrl){
bool cache = false;
CacheManager cm = new CacheManager();
Dictionary<string, string> friendlyUrl = cm.Get(navTermSet_cache_key) as Dictionary<string, string>;
if (friendlyUrl != null)
{
cache = true;
}
else
{
cache = false;
friendlyUrl = GetPagesUrls();
cm.Add(navTermSet_cache_key, friendlyUrl, null, DateTime.UtcNow.AddMinutes(30), TimeSpan.Zero);
}
if (friendlyUrl != null && friendlyUrl.Count > 0)
{
string url = pageUrl.Substring(pageUrl.IndexOf("Pages"));
string urlConvivial = string.Empty;
KeyValuePair<string, string> kvp = friendlyUrl.FirstOrDefault(k => k.Key.Equals(url));
if (!string.IsNullOrEmpty(kvp.Value))
{
urlConvivial = kvp.Value;
}
if(urlConvivial.StartsWith("/"))
{
//Cas ou l'url convivial est présente (ex: "/conseilPratiques")
return string.Format("{0}{1}", SPContext.Current.Site.Url, urlConvivial);
}
else
{
//Cas ou l'url convivial n'est pas présente (ex: "Pages/conseilPratiques")
return string.Format("{0}/{1}", SPContext.Current.Site.Url, urlConvivial);
}
}
else
{
//cas bateau ne devrait pas rentrer dans ce cas. si le magasin des termes est bien configuré.
return pageUrl;
}
}
string friendlyUrl = TermManager.ResolveConvivialURL("Pages/article1.aspx");
 
 
Are you trying to earn money from your websites by popup ads?
RépondreSupprimerIf so, did you take a look at Propeller Ads?