Skip to content

Instantly share code, notes, and snippets.

View chrcar01's full-sized avatar

Chris Carter chrcar01

View GitHub Profile
@chrcar01
chrcar01 / jQuery.DataTables.Models.cs
Created January 13, 2024 20:11
jQuery DataTables models
public class DataTableSearch
{
public string? Value
{
get; set;
}
public bool Regex
{
get; set;
}
//Copyright (C) Microsoft Corporation. All rights reserved.
//https://learn.microsoft.com/en-us/previous-versions/bb894665(v=msdn.10)?redirectedfrom=MSDN
//https://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
@chrcar01
chrcar01 / DependencyInjectionConfig.cs
Created October 26, 2023 02:11
MVC 4 Dependency Injection using Microsoft's ServiceProvider
/// <summary>
/// Wires up everything needed for dependency injection in mvc controllers.
/// Ripped from SO: https://stackoverflow.com/a/68873916
/// </summary>
public class DependencyInjectionConfig
{
public static void Configure(ControllerBuilder controllerBuilder)
{
var services = new ServiceCollection();
@chrcar01
chrcar01 / Program.cs
Created October 17, 2023 19:31
Htmx + ASP.NET(.NET 8 RC2) -> Minimal Api Hello World Example
/*
ASP.NET MinimalApi, Htmx Demo
Notes:
* only tested with .NET 8 RC2
* don't do something like this in production, this is just a dumbed down example of how to use Htmx with ASP.NET
1) Create a new web api project:
dotnet new webapi --no-https -n HtmxMinApiDemo
2) Replace the entire contents of the generated Program.cs file with all of this content
/*
* Arguments class: application arguments interpreter
*
* Authors: R. LOPES
* Contributors: R. LOPES
* Created: 25 October 2002
* Modified: 28 October 2002
*
* Version: 1.0
*/
@chrcar01
chrcar01 / AsyncFileStreamMinApi.cs
Last active January 20, 2023 20:41
Async FileStreams: Example of how to produce correct OpenAPI docs for returning files as streams and byte arrays.
async Task Main()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(s => s.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Report API",
Description = "Manager all of your reporting needs.",
Version = "v1",
TermsOfService = null,
/// <summary>
/// Helper for working with calendar concepts.
/// </summary>
public static class CalendarHelper
{
/// <summary>
/// Returns all of the weeks for specified calendar month.
/// </summary>
public static IEnumerable<Week> GetWeeksForCalendarMonth(DateOnly month)
{
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
// https://www.pinvoke.net/default.aspx/shlwapi.assocquerystring
// https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-assocquerystringa
// https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-findexecutablea
// https://stackoverflow.com/a/9540278
@chrcar01
chrcar01 / ProjectSourcePath.cs
Last active March 19, 2022 20:56
Code to find project root path in a visual studio project
using System.Runtime.CompilerServices;
// Ripped from S.O. answer: https://stackoverflow.com/a/66285728/1594171
// 1. Put this code in ProjectSourcePath.cs at root of project
// 2. Reference project path like this: ProjectSourcePath.Value
internal static class ProjectSourcePath
{
private const string myRelativePath = nameof(ProjectSourcePath) + ".cs";
private static string? lazyValue;
public static string Value => lazyValue ??= CalculatePath();
@chrcar01
chrcar01 / GenericComparer.cs
Last active March 31, 2021 14:56
GenericComparer<T>
public class GenericComparer<T> : IComparer<T>
{
private readonly Func<T, IComparable> _comparer;
private GenericComparer(Func<T, IComparable> comparer)
{
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
}
public static GenericComparer<T> Create(Func<T, IComparable> comparer)