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
@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)
@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>
[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,
@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()
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)
);
/// <summary>
/// Sourced from https://benfoster.io/blog/improving-aspnet-mvc-routing-configuration
/// </summary>
public interface IRouteRegistry
{
/// <summary>
/// Provides the global <see cref="RouteCollection"/> to which the Registery's routes
/// should be added
/// </summary>
/// <param name="routes"></param>
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }