Skip to content

Instantly share code, notes, and snippets.

@eka808
eka808 / gist:9728600
Created March 23, 2014 19:37
Render a razor view as a string
protected string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
@eka808
eka808 / gist:9545449
Created March 14, 2014 10:36
Simplest C# event sample ever
void Main()
{
new Foo().DoSomething();
}
// Define other methods and classes here
class Foo
{
public Foo()
{
@eka808
eka808 / gist:8022303
Created December 18, 2013 13:26
download HTTP data
var client = new WebClient();
var content = client.DownloadString("http://example.com");
@eka808
eka808 / gist:7704199
Created November 29, 2013 10:56
ForEach implementation
public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach(T item in enumeration)
{
action(item);
}
}
@eka808
eka808 / gist:5926225
Created July 4, 2013 09:24
Make the Set entity framework method usable in unit tests
/// <summary>
/// Overload of the Set method of entity framework
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IDbSet<T> Set<T>() where T : class
{
var correspondingDbSets = this.GetType().GetProperties().Where(a => a.PropertyType == typeof(IDbSet<T>));
if (correspondingDbSets.Count() == 1)