Adding EntryAssembly to AddRazorPages/AddControllers*
See original GitHub issueBackground and Motivation
Currently, the default behavior in MVC to load the default ApplicationParts
from the entryassembly
is rely on the IWebHostEnvironment
, however, as reported in the #10611, there are scenarios where the developer wants to just create a new ServiceCollection
and configure as needed. This scenario is not supported today because some providers will not be loaded, eg. RazorCompiledItemProvider
required in Razor Pages.
The proposal is to add a new overload to all Add*
methods available to include Assembly entryAssembly
parameter that will be used to load the default ApplicationParts
from.
Related:
Proposed API
namespace Microsoft.Extensions.DependencyInjection
{
public static class MvcServiceCollectionExtensions
{
+ public static IMvcBuilder AddControllers(this IServiceCollection services, Assembly entryAssembly)
+ {}
+ public static IMvcBuilder AddControllers(this IServiceCollection services, Assembly entryAssembly, Action<MvcOptions>? configure)
+ {}
+ public static IMvcBuilder AddControllersWithViews(this IServiceCollection services, Assembly entryAssembly)
+ {}
+ public static IMvcBuilder AddControllersWithViews(this IServiceCollection services, Assembly entryAssembly, Action<MvcOptions>? configure)
+ {}
+ public static IMvcBuilder AddRazorPages(this IServiceCollection services, Assembly entryAssembly)
+ {}
+ public static IMvcBuilder AddRazorPages(this IServiceCollection services, Assembly entryAssembly, Action<RazorPagesOptions>? configure)
+ {}
}
}
namespace Microsoft.Extensions.DependencyInjection
{
public static class MvcCoreServiceCollectionExtensions
{
+ public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services, Assembly entryAssembly)
+ {}
}
}
Usage Examples
RazorPages
services.AddRazorPages(typeof(Startup).Assembly);
Controllers
services.AddControllers(typeof(Startup).Assembly);
Controllers with views
services.AddControllersWithViews(typeof(Startup).Assembly);
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
Share controllers, views, Razor Pages and more with ...
Using Application Parts, you can share an assembly (DLL) containing controllers, views, Razor Pages, razor compilation sources, Tag Helpers, and ...
Read more >Adding asp.net controllers/views from a Class Library
var assembly = typeof(AnySharedController).Assembly; builder.Services.AddRazorPages() .AddJsonOptions(x => x.JsonSerializerOptions.
Read more >AddController vs AddMvc vs AddControllersWithViews ...
In this article, I am going to discuss the AddController() vs AddMvc() vs AddControllersWithViews() vs AddRazorPages() methods.
Read more >ASP.NET Core MVC 3.x – AddMvc(), AddMvcCore ...
In ASP.NET Core 3.x, aside from those two approaches, there are three additional ways: services.AddControllers(); services.
Read more >Exploring .NET Core 3.0. What's New?
This command will create a single file package for your Windows 10 application. Alternatively, you can edit your project file by adding the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@dazinator After some discussion we decided that this API is not totally correct, and we should not add it, right now. What I will add instead, for now, is just a log message informing that no actions were detected and could be caused by a misconfiguration. Also point to this documentation Share controllers, views, Razor Pages and more with Application Parts that explains the usage of
AddApplicationPart
andConfigureApplicationPartManager
Makes sense - just wanted to mention that if previous method remains, then it may be prudent to throw an exception when that method cannot locate the entry assembly due to missing prerequisite services, perhaps with the error indicating that you can call the new overload to provide the entry assembly as a param explicitly - that should catch cases like the issue linked?