Skip to content

Instantly share code, notes, and snippets.

@karlitros
karlitros / OpenPop.cs
Created June 19, 2012 16:15
OpenPOP source code for fetching email from a POP server
OpenPOP.POP3.POPClient client = new POPClient("pop.yourserver.co.uk", 110, "[email protected]", "password_goes_here", AuthenticationMethod.USERPASS);
if (client.Connected) {
int msgCount = client.GetMessageCount();
/* Cycle through messages */
for (int x = 0; x < msgCount; x++)
{
OpenPOP.MIMEParser.Message msg = client.GetMessage(x, false);
@karlitros
karlitros / global-snippet.asax.cs
Created June 20, 2012 10:24
Avoiding duplicate content due to case-sensitive URLs
protected void Application_BeginRequest(object sender, EventArgs e)
{
//If upper case letters are found in the URL, redirect to lower case URL.
if (Regex.IsMatch(HttpContext.Current.Request.RawUrl.ToString(), @"[A-Z]") == true)
{
if (Request.HttpMethod == "GET" && !(Request.RawUrl.Contains(".axd")))
{
string LowercaseURL = HttpContext.Current.Request.RawUrl.ToString().ToLower();
Response.Clear();
Response.Status = "301 Moved Permanently";
@karlitros
karlitros / RefreshDocument.cs
Created June 28, 2012 08:06
A fix to allow document urls to update in the Umbraco back office after moving a document programmatically
//newParentFolderId = the parentId you want to move the document to
//documentObject = the document you're moving to its new location.
documentObject.Move(newParentFolderId);
if (!autoFolder.EnableBatchProcessing)
{
library.RefreshContent();
documentObject.Publish(new User(0));
@karlitros
karlitros / BroadcastUpdate.cs
Created July 19, 2012 15:27
Broadcasts the update to all connected clients using SignalR. I've done this within the controller to demonstrate server -> client pushing.
/// <summary>
/// Broadcasts the update to all connected clients. I've done this directly in the controller to
/// demonstrate server -> client pushing, rather than client -> client pushing.
/// </summary>
/// <param name="updateItem">The update item.</param>
internal static void BroadcastUpdate(Update updateItem)
{
// Fetch the hub's context to broadcast
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<LiveUpdate.Hubs.LiveUpdateHub>();
@karlitros
karlitros / LiveUpdateHub.cs
Created July 19, 2012 15:35
Hub class for SignalR communication between client and server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalR.Hubs;
namespace LiveUpdate.Hubs
{
/// <summary>
/// Skeleton hub class used to establish connections.
@karlitros
karlitros / LiveUpdate.js
Created July 19, 2012 15:38
SignalR Javascript to allow communication between client and server.
/// <reference path="jquery-1.7.2.js" />
/// <reference path="jquery-ui-1.8.20.js" />
/// <reference path="jquery.signalR-0.5.2.js" />
$(function () {
// Establish a connection to the updateFeed hub
var hub = $.connection.updateFeed;
// Extend the object with our feedUpdated method held within the updateFeed hub
$.extend(hub, {
@karlitros
karlitros / GetCellReferenceByCell
Created August 15, 2012 12:49
A function for NPOI that, when provided with an object implementing ICell, will return the cell reference (A1, B2, C3, etc)
/// <summary>
/// Gets the cell reference (A1, B2, etc) by object implementing ICell.
/// </summary>
/// <param name="cell">The cell.</param>
/// <returns>cell reference as string</returns>
private string GetCellReferenceByCell(ICell cell)
{
CellReference cellRef = new CellReference(cell);
return cellRef.FormatAsString();
}
@karlitros
karlitros / Old_No_Logging_Global.asax.cs
Created October 19, 2012 11:39
The old global.asax.cs file that doesn't allow logging when customErrors is switched on.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
@karlitros
karlitros / New_With_Elmah_Logging_Global.asax.cs
Created October 19, 2012 11:42
New reference to the ElmahHandledErrorLoggerFilter class, which raises the errors to Elmah when customErrors is switched on.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ElmahHandledErrorLoggerFilter());
filters.Add(new HandleErrorAttribute());
}
@karlitros
karlitros / ElmahHandledErrorLoggerFilter.cs
Created October 19, 2012 11:45
Ensures that errors are raised by Elmah in an MVC 3 application. This is referenced in the global.asax.cs file.
public class ElmahHandledErrorLoggerFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
// Log only handled exceptions, because all other will be caught by ELMAH anyway.
if (context.ExceptionHandled)
ErrorSignal.FromCurrentContext().Raise(context.Exception);
}
}