Skip to content

Instantly share code, notes, and snippets.

View csharpforevermore's full-sized avatar
🏠
Working from home

Randle csharpforevermore

🏠
Working from home
View GitHub Profile
@csharpforevermore
csharpforevermore / Class1.cs
Created August 9, 2013 17:28
In Umbraco (or any other ASP.NET application using Log4Net, we can output to the log file using the following example. Example
public class Class1
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public void MyMethod1()
{
// do something
Log.Debug("Example log message");
Log.Warn("This is a warning.");
}
@csharpforevermore
csharpforevermore / Entry.cs
Created August 22, 2013 10:23
An example model class for Entity Framework using code first
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Website.Models.EntityFrameworkCodeFirst
{
public class Entry
{
[Key]
@csharpforevermore
csharpforevermore / showModelStateErrors.cs
Created August 22, 2013 10:24
When your Action has the condition "ModelState.IsValid = false", you sometimes need to know what is failing. In this case, use the following to iterate through your MVC errors.
foreach (ModelState state in ViewData.ModelState.Values.Where(x => x.Errors.Count > 0))
{
var setbreakpointhere = 1;
}
@csharpforevermore
csharpforevermore / StringToMemoryStream.cs
Created September 4, 2013 16:47
Output a string to a memory stream
MemoryStream mStream =
new MemoryStream(ASCIIEncoding.Default.GetBytes("Your string here"));
@csharpforevermore
csharpforevermore / ShuffleList.cs
Last active December 22, 2015 16:28
Randomize / Randomise an ienumerable collection
IEnumerable<int> ints;
var random = new Random();
var shuffled = ints.OrderBy(i => random.Next()).ToList();
// or we can avoid risks of using OrderBy by declaring our own
var random = new Random();
var shuffled = ints
@csharpforevermore
csharpforevermore / GetRandomChildren.cs
Created September 9, 2013 18:13
In Umbraco Razor, sometimes it is necessary to get a random few nodes. This example allows you to get a random collection of child nodes.
// take specific amount from randomised collection of the children
var children = CurrentModel.Children;
var random = new Random();
var shuffled = children.OrderBy(i => random.Next()).ToList().Take(childCount);
children = new DynamicNodeList(shuffled);
@using uComponents.DataTypes.MultiUrlPicker.Dto
@using uComponents.DataTypes.UrlPicker
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
const string propertyAlias = "propertyAlias";
MultiUrlPickerState links = null;
string value = CurrentModel.GetPropertyValue(propertyAlias);
if (!string.IsNullOrEmpty(value))
{
links = MultiUrlPickerState.Deserialize(value);
@csharpforevermore
csharpforevermore / GetDaySuffix.cs
Created October 25, 2013 11:32
Returns the text that comes after a digit in a date (e.g. 1 = 1st, 15 = 15th, 23 = 23rd, etc)
public static string GetDaySuffix(int day)
{
switch (day)
{
case 1:
case 21:
case 31:
return "st";
case 2:
case 22:
@csharpforevermore
csharpforevermore / GetApiController.cs
Created October 25, 2013 11:40
Get the URL for a specific Umbraco API Controller (in this example, the Products API controller)
@{
Layout = null;
}
@{
string url = string.Format("{0}://{1}{2}?nodeId={3}", Request.Url.Scheme, Request.Url.Authority, Url.GetUmbracoApiService<ProductsApiController>("GetProductById"), Model.Content.Id);
}
<a href="@url" target="_blank">@url</a>
@csharpforevermore
csharpforevermore / FindRelativePathsToFilteredFolderFiles.cs
Created October 25, 2013 15:33
ASP.NET - get relative path to files in a specific folder
private IEnumerable<string> GetRelativePathsToRoot(string virtualPath, string filter)
{
var physicalPath = Server.MapPath(virtualPath);
var absolutePaths = Directory.EnumerateFiles(
physicalPath,
filter,
SearchOption.AllDirectories
);
return absolutePaths.Select(
x => Url.Content(virtualPath + x.Replace(physicalPath, ""))