Skip to content

Instantly share code, notes, and snippets.

View seangwright's full-sized avatar
🐻
Still learning...

Sean G. Wright seangwright

🐻
Still learning...
View GitHub Profile
public static class PageTypeRouteAttributeCacheHelper
{
public static readonly Dictionary<string, ControllerActionPair> ClassNameLookup;
static PageTypeRouteAttributeCacheHelper()
{
var controllerTypes = typeof(PageTypeRouteAttributeCacheHelper)
.Assembly
.GetTypes()
.Where(t =>
@seangwright
seangwright / DependencyResolverConfig.cs
Last active July 10, 2019 00:02
Kentico 12: Design Patterns Part 4 - Adding Dependency Injection to the CMS
public static class DependencyResolverConfig
{
public static IContainer BuildContainer()
{
var builder = new ContainerBuilder();
// My registration classes use a "fluent" interface, always returning the ContainerBuilder
// so the next call can be chained off the previous
var container = builder
.RegisterApplicationTypes()
@seangwright
seangwright / KenticoMvcApp.csproj
Last active June 26, 2019 15:45
WebApi2 Swashbuckle integration
<!-- Add this to ensure the XML comments are picked up by the Swashbuckle integration -->
<PropertyGroup>
<DocumentationFile>bin\KenticoMvcApp.xml</DocumentationFile>
</PropertyGroup>
@seangwright
seangwright / JsonDotNetValueProviderFactory.cs
Created June 26, 2019 06:12
Custom Json Serialization for ASP.NET MVC
/// <summary>
/// From https://github.com/garysharp/Disco/commit/1dfa3f4f15fe4fc093e90e4cd490dd06cc30cf07
/// </summary>
public class JsonDotNetValueProviderFactory : ValueProviderFactory
{
private readonly JsonSerializerSettings serializerSettings;
public JsonDotNetValueProviderFactory(JsonSerializerSettings serializerSettings)
{
Guard.Against.Null(serializerSettings, nameof(serializerSettings));
@seangwright
seangwright / ApiExceptionResult.cs
Last active June 26, 2019 05:58
Error Handling in Web Api 2
public class ApiExceptionResult : IHttpActionResult
{
private static readonly MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/json");
private readonly ApiError errorResponse;
private readonly JsonMediaTypeFormatter jsonMediaTypeFormatter;
private readonly HttpRequestMessage requestMessage;
private readonly HttpStatusCode statusCode;
public ApiExceptionResult(HttpRequestMessage requestMessage, HttpStatusCode statusCode, ApiError errorResponse, JsonMediaTypeFormatter jsonMediaTypeFormatter)
[TextFixture]
public class UserInfoTests : UnitTests
{
[Test]
public void IsAuthorized_Will_Return_True_When_User_Is_In_All_Roles(
int userId,
string userName,
int siteId,
string siteName,
int roleId1,
public class ProductsController : Controller
{
private readonly IProductsQuery productsQuery;
private readonly IProductDetailQuery productDetailQuery;
public ProductsController(
IProductsQuery productsQuery,
IProductDetailQuery productDetailQuery)
{
// ...
/// DependencyConfiguration.cs
/// --------------------------
// ContainerBuilder builder from Autofac
builder.RegisterAssemblyTypes(assemblies)
.Where(t => t.IsClass && !t.IsAbstract && t.Name.EndsWith("RouteRegistry"))
.AsImplementedInterfaces();
/// RouteConfig.cs
/// --------------
public static class RouteCollectionExtensions
{
public static void MapRoute<T>(
this RouteCollection routes,
string name,
string url,
Func<T, string> nameOfDefaultAction) where T : Controller
{
string controller = typeof(T).RemoveControllerSuffix();
string action = nameOfDefaultAction(default);
public class HomeRouteRegistry : IRouteRegistry
{
public int Priority => RoutePriorityReference.LOWEST;
public void RegisterRoutes(RouteCollection routes) =>
routes.MapRoute<HomeController>(
name: "Home",
url: "",
nameOfDefaultAction: c => nameof(c.Index)
);