csharp Umbraco属性的一组有用的扩展方法。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Umbraco属性的一组有用的扩展方法。相关的知识,希望对你有一定的参考价值。
namespace Client.Web
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using Newtonsoft.Json;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;
/// <summary>
/// Extension methods for BrambleBerry.Base models
/// </summary>
public static class ContentExtensions
{
/// <summary>
/// Checks if the model has a property and a value for the property
/// </summary>
/// <param name="model">The <see cref="RenderModel"/></param>
/// <param name="propertyAlias">The Umbraco property alias on the model</param>
/// <returns>A value indicating whether or not the property exists on the model and has a value</returns>
public static bool WillWork(this RenderModel model, string propertyAlias)
{
return model.Content.WillWork(propertyAlias);
}
/// <summary>
/// Checks if the model has a property and a value for the property
/// </summary>
/// <param name="model">
/// The <see cref="IPublishedContent"/> to inspect
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias on the <see cref="IPublishedContent"/>
/// </param>
/// <returns>
/// A value indicating whether or not the property exists on the <see cref="IPublishedContent"/> and has a value
/// </returns>
public static bool WillWork(this IPublishedContent model, string propertyAlias)
{
return model.HasProperty(propertyAlias) && model.HasValue(propertyAlias);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the string representation
/// of the property or an empty string
/// </summary>
/// <param name="model">
/// The <see cref="RenderModel"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias
/// </param>
/// <returns>
/// The property value as a string or an empty string
/// </returns>
public static string GetSafeString(this RenderModel model, string propertyAlias)
{
return model.Content.GetSafeString(propertyAlias);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the string representation
/// of the property or an empty string
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/> that should contain the property
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetSafeString(this IPublishedContent content, string propertyAlias)
{
return content.GetSafeString(propertyAlias, string.Empty);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the string representation
/// of the property or the default value
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/> that should contain the property
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <param name="defaultValue">
/// The default value.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string GetSafeString(this IPublishedContent content, string propertyAlias, string defaultValue)
{
return content.WillWork(propertyAlias) ? content.GetPropertyValue<string>(propertyAlias) : defaultValue;
}
/// <summary>
/// Gets a safe date time from content
/// </summary>
/// <param name="content">
/// The content.
/// </param>
/// <param name="propertyAlias">
/// The property alias.
/// </param>
/// <returns>
/// The <see cref="DateTime"/>.
/// </returns>
public static DateTime GetSafeDateTime(this IPublishedContent content, string propertyAlias)
{
return content.GetSafeDateTime(propertyAlias, DateTime.MinValue);
}
/// <summary>
/// Gets a safe date time from content
/// </summary>
/// <param name="content">
/// The content.
/// </param>
/// <param name="propertyAlias">
/// The property alias.
/// </param>
/// <param name="defaultValue">
/// The default value.
/// </param>
/// <returns>
/// The <see cref="DateTime"/>.
/// </returns>
public static DateTime GetSafeDateTime(this IPublishedContent content, string propertyAlias, DateTime defaultValue)
{
if (!content.WillWork(propertyAlias)) return defaultValue;
DateTime dt;
return DateTime.TryParse(content.GetPropertyValue<string>(propertyAlias), out dt) ? dt : defaultValue;
}
/// <summary>
/// Gets a safe date time from content.
/// </summary>
/// <param name="model">
/// The <see cref="RenderModel"/>.
/// </param>
/// <param name="propertyAlias">
/// The property alias.
/// </param>
/// <returns>
/// The <see cref="DateTime"/>.
/// </returns>
public static DateTime GetSafeDateTime(this RenderModel model, string propertyAlias)
{
return model.Content.GetSafeDateTime(propertyAlias);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the Guid representation
/// of the property or the default value
/// </summary>
/// <param name="model">
/// The <see cref="RenderModel"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="Guid"/>.
/// </returns>
public static Guid GetSafeGuid(this RenderModel model, string propertyAlias)
{
return model.Content.GetSafeGuid(propertyAlias, Guid.Empty);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the Guid representation
/// of the property or the default value
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/>.
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="Guid"/>.
/// </returns>
public static Guid GetSafeGuid(this IPublishedContent content, string propertyAlias)
{
return content.GetSafeGuid(propertyAlias, Guid.Empty);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the Guid representation
/// of the property or the default value
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/>.
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <param name="defaultValue">
/// The default Value.
/// </param>
/// <returns>
/// The <see cref="Guid"/>.
/// </returns>
public static Guid GetSafeGuid(this IPublishedContent content, string propertyAlias, Guid defaultValue)
{
return content.WillWork(propertyAlias)
? new Guid(content.GetPropertyValue<string>(propertyAlias))
: defaultValue;
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the string representation
/// of the property or the default value of 0
/// </summary>
/// <param name="model">
/// The <see cref="RenderModel"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
public static int GetSafeIntenger(this RenderModel model, string propertyAlias)
{
return model.Content.GetSafeInteger(propertyAlias);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the string representation
/// of the property or the default value of 0
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
public static int GetSafeInteger(this IPublishedContent content, string propertyAlias)
{
return content.GetSafeInteger(propertyAlias, 0);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the string representation
/// of the property or the default value of 0
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <param name="defaultValue">
/// The default Value.
/// </param>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
public static int GetSafeInteger(this IPublishedContent content, string propertyAlias, int defaultValue)
{
return content.WillWork(propertyAlias) ? content.GetPropertyValue<int>(propertyAlias) : defaultValue;
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the <see cref="IHtmlString"/> representation
/// of the property or an empty <see cref="IHtmlString"/>
/// </summary>
/// <param name="model">
/// The <see cref="RenderModel"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="IHtmlString"/>.
/// </returns>
public static IHtmlString GetSafeHtmlString(this RenderModel model, string propertyAlias)
{
return model.Content.GetSafeHtmlString(propertyAlias);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the <see cref="IHtmlString"/> representation
/// of the property or an empty <see cref="IHtmlString"/>
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/> that should contain the property
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="IHtmlString"/>.
/// </returns>
public static IHtmlString GetSafeHtmlString(this IPublishedContent content, string propertyAlias)
{
return content.GetSafeHtmlString(propertyAlias, string.Empty);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the <see cref="IHtmlString"/> representation
/// of the property or the default <see cref="IHtmlString"/>
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/> that should contain the property
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias
/// </param>
/// <param name="defaultValue">
/// The default value.
/// </param>
/// <returns>
/// The <see cref="IHtmlString"/>.
/// </returns>
public static IHtmlString GetSafeHtmlString(this IPublishedContent content, string propertyAlias, string defaultValue)
{
return content.WillWork(propertyAlias)
? content.GetPropertyValue<IHtmlString>(propertyAlias)
: MvcHtmlString.Create(defaultValue);
}
#region IImage
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the <see cref="IImage"/> representation
/// of the property or the default <see cref="IImage"/>
/// </summary>
/// <param name="model">
/// The <see cref="RenderModel"/>
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="IImage"/>.
/// </returns>
public static IPublishedContent GetSafeImage(this RenderModel model, UmbracoHelper umbraco, string propertyAlias)
{
return model.Content.GetSafeImage(umbraco, propertyAlias);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the <see cref="IImage"/> representation
/// of the property or the default <see cref="IImage"/>
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/>
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <returns>
/// The <see cref="IImage"/>.
/// </returns>
public static IPublishedContent GetSafeImage(this IPublishedContent content, UmbracoHelper umbraco, string propertyAlias)
{
return content.GetSafeImage(umbraco, propertyAlias, null);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the <see cref="IImage"/> representation
/// of the property or the default <see cref="IImage"/>
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/>
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property alias.
/// </param>
/// <param name="defaultImage">
/// The default image.
/// </param>
/// <returns>
/// The <see cref="IImage"/>.
/// </returns>
public static IPublishedContent GetSafeImage(this IPublishedContent content, UmbracoHelper umbraco, string propertyAlias, IImage defaultImage)
{
return content.GetSafeImages(umbraco, propertyAlias, defaultImage).FirstOrDefault();
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the <see cref="IImage"/> representation
/// of the property or the default <see cref="IImage"/>s
/// </summary>
/// <param name="model">
/// The <see cref="RenderModel"/> which has the media picker property
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>
/// </param>
/// <param name="propertyAlias">
/// The property alias of the media picker
/// </param>
/// <returns>
/// A collection of <see cref="IImage"/>.
/// </returns>
public static IEnumerable<IPublishedContent> GetSafeImages(this RenderModel model, UmbracoHelper umbraco, string propertyAlias)
{
return model.Content.GetSafeImages(umbraco, propertyAlias, null);
}
/// <summary>
/// Checks if the model has a property and a value for the property and returns either the <see cref="IImage"/> representation
/// of the property or the default <see cref="IImage"/>s
/// </summary>
/// <param name="content">
/// The content which has the media picker property
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>
/// </param>
/// <param name="propertyAlias">
/// The property alias of the media picker
/// </param>
/// <param name="defaultImage">
/// A default image to return if there are no results
/// </param>
/// <returns>
/// A collection of <see cref="IImage"/>.
/// </returns>
public static IEnumerable<IPublishedContent> GetSafeImages(this IPublishedContent content, UmbracoHelper umbraco,string propertyAlias, IPublishedContent defaultImage)
{
var mediaContent = content.GetSafeMntpPublishedContent(umbraco, propertyAlias, true).ToArray();
return mediaContent.Any()
? mediaContent.Where(x => x != null)
: new List<IPublishedContent>() { defaultImage };
}
#endregion
/// <summary>
/// Gets a content Id from a content picker and renders it as <see cref="IPublishedContent"/>.
/// </summary>
/// <param name="model">
/// The current <see cref="RenderModel"/>.
/// </param>
/// <param name="propertyAlias">
/// The property alias.
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>.
/// </param>
/// <returns>
/// The <see cref="IPublishedContent"/> from the content picker.
/// </returns>
public static IPublishedContent GetSafeContent(this RenderModel model, string propertyAlias, UmbracoHelper umbraco)
{
return model.Content.GetSafeContent(propertyAlias, umbraco);
}
/// <summary>
/// Check to see if all the properties listed exist and have a value
/// </summary>
/// <param name="model">
/// The <see cref="IPublishedContent"/> current page.
/// </param>
/// <param name="properties">
/// A list of property names.
/// </param>
/// <returns></returns>
public static bool AllWork(this IPublishedContent model, string[] properties)
{
var works = true;
foreach (var property in properties)
{
works = model.WillWork(property);
}
return works;
}
/// <summary>
/// Check to see if any of the properties listed exist and have a value
/// </summary>
/// <param name="model">
/// The <see cref="IPublishedContent"/> current page.
/// </param>
/// <param name="properties">
/// A list of property names.
/// </param>
/// <returns></returns>
public static bool AnyWork(this IPublishedContent model, string[] properties)
{
return properties.Any(model.WillWork);
}
/// <summary>
/// Gets a content Id from a content picker and renders it as <see cref="IPublishedContent"/>.
/// </summary>
/// <param name="content">
/// The current <see cref="IPublishedContent"/>.
/// </param>
/// <param name="propertyAlias">
/// The property alias.
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>.
/// </param>
/// <returns>
/// The <see cref="IPublishedContent"/> from the content picker.
/// </returns>
public static IPublishedContent GetSafeContent(this IPublishedContent content, string propertyAlias, UmbracoHelper umbraco)
{
return content.WillWork(propertyAlias)
? umbraco.TypedContent(content.GetPropertyValue(propertyAlias))
: null;
}
#region MNTP
/// <summary>
/// Creates a collection of <see cref="IPublishedContent"/> of either content or media based on values saved by an Umbraco MultiNodeTree Picker DataType
/// </summary>
/// <param name="model">
/// The <see cref="RenderModel"/>
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property Alias.
/// </param>
/// <param name="isMedia">
/// True or false indicating whether or not the property is an Umbraco media item
/// </param>
/// <returns>
/// The collection of <see cref="IPublishedContent"/>.
/// </returns>
public static IEnumerable<IPublishedContent> GetSafeMntpPublishedContent(this RenderModel model, UmbracoHelper umbraco, string propertyAlias, bool isMedia = false)
{
return model.Content.GetSafeMntpPublishedContent(umbraco, propertyAlias, isMedia);
}
/// <summary>
/// Creates a collection of <see cref="IPublishedContent"/> of either content or media based on values saved by an Umbraco MultiNodeTree Picker DataType
/// </summary>
/// <param name="content">
/// The <see cref="IPublishedContent"/>
/// </param>
/// <param name="umbraco">
/// The <see cref="UmbracoHelper"/>
/// </param>
/// <param name="propertyAlias">
/// The Umbraco property Alias.
/// </param>
/// <param name="isMedia">
/// True or false indicating whether or not the property is an Umbraco media item
/// </param>
/// <returns>
/// The collection of <see cref="IPublishedContent"/>.
/// </returns>
public static IEnumerable<IPublishedContent> GetSafeMntpPublishedContent(this IPublishedContent content, UmbracoHelper umbraco, string propertyAlias, bool isMedia = false)
{
if (!content.WillWork(propertyAlias)) return new IPublishedContent[] { };
var ids = content.GetPropertyValue<string>(propertyAlias).Split(',');
if (!ids.Any()) return new IPublishedContent[] { };
return isMedia ? umbraco.TypedMedia(ids) : umbraco.TypedContent(ids);
}
#endregion
public static string RemovePort(this string currentUrl)
{
Uri oldUri = new Uri(currentUrl);
UriBuilder builder = new UriBuilder(oldUri) { Port = -1 };
Uri newUri = builder.Uri;
return newUri.ToString();
}
}
}
以上是关于csharp Umbraco属性的一组有用的扩展方法。的主要内容,如果未能解决你的问题,请参考以下文章