Skip to content

Instantly share code, notes, and snippets.

View stefanolsen's full-sized avatar

Stefan Holm Olsen stefanolsen

View GitHub Profile
@stefanolsen
stefanolsen / UrlExtensions.cs
Last active July 10, 2017 14:25
UrlExtension for rendering a Content URL to a physical file, adding the modification date as a URL parameter. Explanation at https://stefanolsen.com/posts/cache-busting-with-aspnet-mvc/
public static class UrlExtensions
{
private const string FileDateTicksCacheKeyFormat = "FileDateTicks_{0}";
private static long GetFileDateTicks(this UrlHelper urlHelper, string filename)
{
var context = urlHelper.RequestContext.HttpContext;
string cacheKey = string.Format(FileDateTicksCacheKeyFormat, filename);
// Check if we already cached the ticks in the cache.
public class AccountController : Controller
{
public IActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model)
@stefanolsen
stefanolsen / AuthorizeModuleAttribute.cs
Last active August 5, 2017 13:02
UrlExtension for authorizing controllers and actions against a list of modules. Blog post at https://stefanolsen.com/posts/using-authorizeattribute-for-modules-in-a-multi-tenancy-web-platform/
public class AuthorizeModuleAttribute : AuthorizeAttribute
{
private readonly ModuleService _moduleService;
public string ModuleKey { get; set; }
public AuthorizeModuleAttribute()
{
_moduleService = new ModuleService();
}
@stefanolsen
stefanolsen / ContentReference.cshtml
Last active August 6, 2017 14:48
Code listings for blog post about rendering EPiServer CMS properties with dynamic HTML attributes. Read about it here: https://stefanolsen.com/posts/adding-dynamic-html-to-property-templates-in-episerver/
@model EPiServer.Core.ContentReference
@Html.ContentLink(Model, null, Html.LinkAttributes(Model))
@stefanolsen
stefanolsen / HandleErrorFriendlyAttribute.cs
Last active August 27, 2017 11:33
Code listings for blog post about generically handling exceptions in EPiServer, showing stack trace to those allowed to see them. Read about it here: https://stefanolsen.com/posts/a-custom-exception-handler-for-episerver-pages/
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public sealed class HandleErrorFriendlyAttribute : HandleErrorAttribute
{
internal Injected<LocalizationService> LocalizationService;
internal Injected<IPageRouteHelper> PageRouteHelper;
internal Injected<PageViewContextFactory> PageViewContextFactory;
internal Injected<PermissionService> PermissionService;
public string ErrorMessageTranslationKey { get; set; }
@stefanolsen
stefanolsen / EnsureAjaxLanguageMvcAttribute.cs
Last active September 12, 2017 10:02
Code listings for blog post about generically ensuring the right EPiServer content language for AJAX requests. Read about it here: https://stefanolsen.com/posts/ensuring-the-right-ajax-content-language-using-a-filter-attribute/
public class EnsureAjaxLanguageMvcAttribute : ActionFilterAttribute
{
private readonly Injected<Settings> _settings;
private readonly Injected<IUpdateCurrentLanguage> _updateCurrentLanguage;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (_settings.Service.PageUseBrowserLanguagePreferences)
{
// If the PageUseBrowserLanguagePreferences setting is true, this is already handled everywhere.
@stefanolsen
stefanolsen / GoogleFeedMapping.xml
Last active November 26, 2017 21:50
Code listings for blog post about generating and exporting catalog feeds from EPiServer Commerce. Read about it here: https://stefanolsen.com/posts/on-exporting-product-catalogs-to-google-or-facebook/
<FieldMapping xmlns="http://stefanolsen.com/CatalogFeed.GoogleMerchant/MappingDocument.xsd">
<ContentType CommerceType="CatalogNode">
<Fields>
<MappedField MetaField="GoogleProductCategoryId" FeedField="google_product_category"/>
</Fields>
</ContentType>
<ContentType CommerceType="Product">
<Fields>
<MappedField MetaField="DisplayName" FeedField="title"/>
@stefanolsen
stefanolsen / AddToCart_request.json
Last active July 29, 2018 08:13
GraphQL requests and responses for blog post about using GraphQL with Episerver Commerce. Read about it here: https://stefanolsen.com/posts/using-graphql-with-episerver-commerce/
{
"query":"mutation AddToCartMutation($input: AddToCartInput!) {
addToCart(input: $input) {
cart {
total {
amount
currencyCode
formattedAmount
}
}
@stefanolsen
stefanolsen / SendGridMailService.cs
Last active October 30, 2018 17:54
Code listings for blog post about using SendGrid with Episerver Commerce. Read about it here: https://stefanolsen.com/posts/sending-emails-from-episerver-commerce-using-sendgrid/
using System.Threading.Tasks;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace EPiServer.Reference.Commerce.Site.Features.Mail.Services
{
public class SendGridMailService : ITemplateMailService
{
private readonly ISendGridSettings _settings;
private readonly ISendGridClient _sendGridClient;
@stefanolsen
stefanolsen / DummyEntityListener.cs
Last active January 22, 2019 17:02
Code piece for blog post about applying Azure Application Insights to InRiver iPMC extensions. Read about it here: https://stefanolsen.com/posts/applying-cloud-logging-to-inriver-ipmc-extensions-for-easier-analytics/
public class DummyEntityListener : IEntityListener
{
private LoggingScope _loggingScope;
public inRiverContext Context { get; set; }
public Dictionary<string, string> DefaultSettings => new Dictionary<string, string>
{
{ SettingsConstants.DisableTelemetry, "TRUE" },
{ SettingsConstants.InstrumentationKey, "REPLACE ME" }
};