--- title: Share controllers, views, Razor Pages and more with Application Parts in ASP.NET Core author: rick-anderson description: Share controllers, view, Razor Pages and more with Application Parts in ASP.NET Core ms.author: riande ms.date: 11/11/2019 uid: mvc/extensibility/app-parts --- # Share controllers, views, Razor Pages and more with Application Parts :::moniker range=">= aspnetcore-3.0" By [Rick Anderson](https://twitter.com/RickAndMSFT) [View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/advanced/app-parts) ([how to download](xref:index#how-to-download-a-sample)) An *Application Part* is an abstraction over the resources of an app. Application Parts allow ASP.NET Core to discover controllers, view components, tag helpers, Razor Pages, razor compilation sources, and more. is an Application part. `AssemblyPart` encapsulates an assembly reference and exposes types and compilation references. [Feature providers](#fp) work with application parts to populate the features of an ASP.NET Core app. The main use case for application parts is to configure an app to discover (or avoid loading) ASP.NET Core features from an assembly. For example, you might want to share common functionality between multiple apps. Using Application Parts, you can share an assembly (DLL) containing controllers, views, Razor Pages, razor compilation sources, Tag Helpers, and more with multiple apps. Sharing an assembly is preferred to duplicating code in multiple projects. ASP.NET Core apps load features from . The class represents an application part that's backed by an assembly. ## Load ASP.NET Core features Use the and classes to discover and load ASP.NET Core features (controllers, view components, etc.). The tracks the application parts and feature providers available. `ApplicationPartManager` is configured in `Startup.ConfigureServices`: [!code-csharp[](./app-parts/3.0sample1/WebAppParts/Startup.cs?name=snippet)] The following code provides an alternative approach to configuring `ApplicationPartManager` using `AssemblyPart`: [!code-csharp[](./app-parts/3.0sample1/WebAppParts/Startup2.cs?name=snippet)] The preceding two code samples load the `SharedController` from an assembly. The `SharedController` is not in the app's project. See the [WebAppParts solution](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/advanced/app-parts/3.0sample1/WebAppParts) sample download. ### Include views Use a [Razor class library](xref:razor-pages/ui-class) to include views in the assembly. ### Prevent loading resources Application parts can be used to *avoid* loading resources in a particular assembly or location. Add or remove members of the collection to hide or make available resources. The order of the entries in the `ApplicationParts` collection isn't important. Configure the `ApplicationPartManager` before using it to configure services in the container. For example, configure the `ApplicationPartManager` before invoking `AddControllersAsServices`. Call `Remove` on the `ApplicationParts` collection to remove a resource. The `ApplicationPartManager` includes parts for: * The app's assembly and dependent assemblies. * `Microsoft.AspNetCore.Mvc.ApplicationParts.CompiledRazorAssemblyPart` * `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation` * `Microsoft.AspNetCore.Mvc.TagHelpers`. * `Microsoft.AspNetCore.Mvc.Razor`. ## Feature providers Application feature providers examine application parts and provide features for those parts. There are built-in feature providers for the following ASP.NET Core features: * * * * * `internal class` [RazorCompiledItemFeatureProvider](https://github.com/dotnet/AspNetCore/blob/main/src/Mvc/Mvc.Razor/src/ApplicationParts/RazorCompiledItemFeatureProvider.cs#L14) Feature providers inherit from , where `T` is the type of the feature. Feature providers can be implemented for any of the previously listed feature types. The order of feature providers in the `ApplicationPartManager.FeatureProviders` can impact run time behavior. Later added providers can react to actions taken by earlier added providers. ### Display available features The features available to an app can be enumerated by requesting an `ApplicationPartManager` through [dependency injection](../../fundamentals/dependency-injection.md): [!code-csharp[](./app-parts/sample2/AppPartsSample/Controllers/FeaturesController.cs?highlight=16,25-27)] The [download sample](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/advanced/app-parts/sample2) uses the preceding code to display the app features: ```text Controllers: - FeaturesController - HomeController - HelloController - GenericController`1 - GenericController`1 Tag Helpers: - PrerenderTagHelper - AnchorTagHelper - CacheTagHelper - DistributedCacheTagHelper - EnvironmentTagHelper - Additional Tag Helpers omitted for brevity. View Components: - SampleViewComponent ``` ## Discovery in application parts HTTP 404 errors are not uncommon when developing with application parts. These errors are typically caused by missing an essential requirement for how applications parts are discovered. If your app returns an HTTP 404 error, verify the following requirements have been met: * The `applicationName` setting needs to be set to the root assembly used for discovery. The root assembly used for discovery is normally the entry point assembly. * The root assembly needs to have a reference to the parts used for discovery. The reference can be direct or transitive. * The root assembly needs to reference the Web SDK. The framework has logic that stamps attributes into the root assembly that are used for discovery. :::moniker-end :::moniker range="< aspnetcore-3.0" By [Rick Anderson](https://twitter.com/RickAndMSFT) [View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/advanced/app-parts) ([how to download](xref:index#how-to-download-a-sample)) An *Application Part* is an abstraction over the resources of an app. Application Parts allow ASP.NET Core to discover controllers, view components, tag helpers, Razor Pages, razor compilation sources, and more. is an Application part. `AssemblyPart` encapsulates an assembly reference and exposes types and compilation references. *Feature providers* work with application parts to populate the features of an ASP.NET Core app. The main use case for application parts is to configure an app to discover (or avoid loading) ASP.NET Core features from an assembly. For example, you might want to share common functionality between multiple apps. Using Application Parts, you can share an assembly (DLL) containing controllers, views, Razor Pages, razor compilation sources, Tag Helpers, and more with multiple apps. Sharing an assembly is preferred to duplicating code in multiple projects. ASP.NET Core apps load features from . The class represents an application part that's backed by an assembly. ## Load ASP.NET Core features Use the `ApplicationPart` and `AssemblyPart` classes to discover and load ASP.NET Core features (controllers, view components, etc.). The tracks the application parts and feature providers available. `ApplicationPartManager` is configured in `Startup.ConfigureServices`: [!code-csharp[](./app-parts/sample1/WebAppParts/Startup.cs?name=snippet)] The following code provides an alternative approach to configuring `ApplicationPartManager` using `AssemblyPart`: [!code-csharp[](./app-parts/sample1/WebAppParts/Startup2.cs?name=snippet)] The preceding two code samples load the `SharedController` from an assembly. The `SharedController` is not in the application's project. See the [WebAppParts solution](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/advanced/app-parts/sample1/WebAppParts) sample download. ### Include views Use a [Razor class library](xref:razor-pages/ui-class) to include views in the assembly. ### Prevent loading resources Application parts can be used to *avoid* loading resources in a particular assembly or location. Add or remove members of the collection to hide or make available resources. The order of the entries in the `ApplicationParts` collection isn't important. Configure the `ApplicationPartManager` before using it to configure services in the container. For example, configure the `ApplicationPartManager` before invoking `AddControllersAsServices`. Call `Remove` on the `ApplicationParts` collection to remove a resource. The following code uses to remove `MyDependentLibrary` from the app: [!code-csharp[](./app-parts/sample1/WebAppParts/StartupRm.cs?name=snippet)] The `ApplicationPartManager` includes parts for: * The app's assembly and dependent assemblies. * `Microsoft.AspNetCore.Mvc.TagHelpers`. * `Microsoft.AspNetCore.Mvc.Razor`. ## Application feature providers Application feature providers examine application parts and provide features for those parts. There are built-in feature providers for the following ASP.NET Core features: * [Controllers](xref:Microsoft.AspNetCore.Mvc.Controllers.ControllerFeatureProvider) * [Tag Helpers](xref:Microsoft.AspNetCore.Mvc.Razor.TagHelpers.TagHelperFeatureProvider) * [View Components](xref:Microsoft.AspNetCore.Mvc.ViewComponents.ViewComponentFeatureProvider) Feature providers inherit from , where `T` is the type of the feature. Feature providers can be implemented for any of the previously listed feature types. The order of feature providers in the `ApplicationPartManager.FeatureProviders` can impact run time behavior. Later added providers can react to actions taken by earlier added providers. ### Display available features The features available to an app can be enumerated by requesting an `ApplicationPartManager` through [dependency injection](../../fundamentals/dependency-injection.md): [!code-csharp[](./app-parts/sample2/AppPartsSample/Controllers/FeaturesController.cs?highlight=16,25-27)] The [download sample](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/advanced/app-parts/sample2) uses the preceding code to display the app features: ```text Controllers: - FeaturesController - HomeController - HelloController - GenericController`1 - GenericController`1 Tag Helpers: - PrerenderTagHelper - AnchorTagHelper - CacheTagHelper - DistributedCacheTagHelper - EnvironmentTagHelper - Additional Tag Helpers omitted for brevity. View Components: - SampleViewComponent ``` ## Discovery in application parts HTTP 404 errors are not uncommon when developing with application parts. These errors are typically caused by missing an essential requirement for how applications parts are discovered. If your app returns an HTTP 404 error, verify the following requirements have been met: * The `applicationName` setting needs to be set to the root assembly used for discovery. The root assembly used for discovery is normally the entry point assembly. * The root assembly needs to have a reference to the parts used for discovery. The reference can be direct or transitive. * The root assembly needs to reference the Web SDK. * The ASP.NET Core framework has custom build logic that stamps attributes into the root assembly that are used for discovery. :::moniker-end