/// <summary> /// Adds an <see cref="IViewLocationExpander"/> to <paramref name="descriptors"/>. /// </summary> /// <param name="descriptors">A list of <see cref="ViewLocationExpanderDescriptor"/>.</param> /// <param name="viewLocationExpander">An <see cref="IViewLocationExpander"/> instance.</param> /// <returns>A <see cref="ViewLocationExpanderDescriptor"/> representing the added instance.</returns> public static ViewLocationExpanderDescriptor Add( [NotNull] this IList <ViewLocationExpanderDescriptor> descriptors, [NotNull] IViewLocationExpander viewLocationExpander) { var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpander); descriptors.Add(descriptor); return(descriptor); }
/// <summary> /// Insert an <see cref="IViewLocationExpander"/> in to <paramref name="descriptors"/> at the specified /// <paramref name="index"/>. /// </summary> /// <param name="descriptors">A list of <see cref="ViewLocationExpanderDescriptor"/>.</param> /// <param name="index">The zero-based index at which <paramref name="viewLocationExpander"/> /// should be inserted.</param> /// <param name="viewLocationExpander">An <see cref="IViewLocationExpander"/> instance.</param> /// <returns>A <see cref="ViewLocationExpanderDescriptor"/> representing the added instance.</returns> public static ViewLocationExpanderDescriptor Insert( [NotNull] this IList <ViewLocationExpanderDescriptor> descriptors, int index, [NotNull] IViewLocationExpander viewLocationExpander) { if (index < 0 || index > descriptors.Count) { throw new ArgumentOutOfRangeException(nameof(index)); } var descriptor = new ViewLocationExpanderDescriptor(viewLocationExpander); descriptors.Insert(index, descriptor); return(descriptor); }
public static void AddPluginsMvc( this IServiceCollection services, IViewLocationExpander viewLocationExpander = null) { ServiceProvider serviceProvider = services.BuildServiceProvider(); ILogger logger = serviceProvider .GetService <ILoggerFactory>() .CreateLogger(typeof(IServiceCollectionExtensions)); // Dont use uglycase urls ! services.AddRouting((options) => { options.LowercaseUrls = true; }); IMvcBuilder mvcBuilder = services .AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .AddViewLocalization() .AddDataAnnotationsLocalization() .ConfigureApplicationPartManager(manager => { IApplicationFeatureProvider toRemove = manager .FeatureProviders .First(f => f is MetadataReferenceFeatureProvider); manager.FeatureProviders .Remove(toRemove); manager.FeatureProviders .Add(new ReferencesMetadataReferenceFeatureProvider()); }); IEnumerable <Assembly> assemblies = PluginAssembyLoader.Assemblies; foreach (Assembly assembly in assemblies) { logger.LogDebug( "Adding mvc application part: \"{0}\"", assembly.FullName ); mvcBuilder.AddApplicationPart(assembly); } mvcBuilder.AddRazorOptions(razor => { if (viewLocationExpander != null) { logger.LogDebug( "Replacing default view location expander with: \"{0}\"", viewLocationExpander.GetType().FullName ); razor.ViewLocationExpanders.Clear(); razor.ViewLocationExpanders.Add(viewLocationExpander); } }); IEnumerable <IAddMvcAction> actions = PluginAssembyLoader .GetServices <IAddMvcAction>(); foreach (IAddMvcAction action in actions) { logger.LogDebug( "Executing add mvc action \"{0}\"", action.GetType().FullName ); action.Execute(mvcBuilder); } }