Skip to content

Instantly share code, notes, and snippets.

@agehrke
Last active January 5, 2016 17:59
Show Gist options
  • Save agehrke/8474a321e804fd0af1a0 to your computer and use it in GitHub Desktop.
Save agehrke/8474a321e804fd0af1a0 to your computer and use it in GitHub Desktop.
Disable globally defined Sitecore Web API filters.
/// <summary>
/// Available at url: /sitecore/api/ssc/example1/andreas/{id}/{action}
/// </summary>
[Sitecore.Services.Core.ServicesController("example1.andreas")]
[ClearSitecoreWebApiConfig]
public class MyController : System.Web.Http.ApiController
{
[HttpGet]
public IHttpActionResult MyAction()
{
// Implement
}
}
using System;
using System.Linq;
using System.Web.Http;
namespace YourNamespace
{
/// <summary>
/// Clear Sitecore's Web API config for a <see cref="ApiController"/>.
/// </summary>
/// <remarks>
/// Removes <see cref="System.Web.Http.Filters.IFilter"/> and <see cref="System.Net.Http.Formatting.MediaTypeFormatter"/> set by Sitecore.
/// Note that this means Authorization Filters and Security Policies defined by Sitecore.Services.Client will no longer work for this controller.
/// </remarks>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ClearSitecoreWebApiConfigAttribute : Attribute, System.Web.Http.Controllers.IControllerConfiguration
{
public void Initialize(System.Web.Http.Controllers.HttpControllerSettings controllerSettings, System.Web.Http.Controllers.HttpControllerDescriptor controllerDescriptor)
{
// Remove Sitecore's Sitecore.Services.Infrastructure.Web.Http.Formatting.BrowserJsonFormatter configured in
// Sitecore.Services.Infrastructure.Sitecore.ApplicationContainer.ResolveServicesWebApiConfiguration().
var sitecoreFormatter = controllerSettings.Formatters.FirstOrDefault(formatter => formatter.GetType() == typeof(Sitecore.Services.Infrastructure.Web.Http.Formatting.BrowserJsonFormatter));
if (sitecoreFormatter != null) controllerSettings.Formatters.Remove(sitecoreFormatter);
// Remove the ConfigurationFilterProvider as this is responsible for providing the globally configured Sitecore filters
var filterProviders = controllerSettings.Services.GetFilterProviders();
var configurationFilterProvider = filterProviders.FirstOrDefault(provider => provider.GetType() == typeof(System.Web.Http.Filters.ConfigurationFilterProvider));
if (configurationFilterProvider != null)
{
controllerSettings.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), configurationFilterProvider);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment