Skip to content

Instantly share code, notes, and snippets.

View stefanolsen's full-sized avatar

Stefan Holm Olsen stefanolsen

View GitHub Profile
@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 / 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 / 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 / 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 / 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)