Hyperar.AddFeatureFolders 1.0.1

dotnet add package Hyperar.AddFeatureFolders --version 1.0.1                
NuGet\Install-Package Hyperar.AddFeatureFolders -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hyperar.AddFeatureFolders" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hyperar.AddFeatureFolders --version 1.0.1                
#r "nuget: Hyperar.AddFeatureFolders, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hyperar.AddFeatureFolders as a Cake Addin
#addin nuget:?package=Hyperar.AddFeatureFolders&version=1.0.1

// Install Hyperar.AddFeatureFolders as a Cake Tool
#tool nuget:?package=Hyperar.AddFeatureFolders&version=1.0.1                

AddFeatureFolders

Build status

Installation

    Install-Package Hyperar.AddFeatureFolders 

Usage

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                    .AddFeatureFolders();

            // "Features" is the default feature folder root. To override, pass along 
            // a new FeatureFolderOptions object with a different FeatureFolderName
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseDeveloperExceptionPage();
            app.UseMvcWithDefaultRoute();
        }
    }    

Now you can organize controllers and views in a Features folder hierarchy

See the sample folder for more examples.

\Features \Home \HomeController.cs \HomeViewModel.cs \HomeIndexHandler.cs \HomeIndexQuery.cs \Index.cshtml

Important!

AddFeatureFolders uses the namespace of the controller to figure out where the views are. For example:

/Features
    /Robots
        /Robots.cshtml

The above example folder structure relies on the namespace of the controller being <whatever>.Features.Robots.

If you encounter problems with MVC locating the views, check your controller namespace.

Disclaimer

Your feature folder name (FeatureFolderOptions.FeatureFolderName or AreaFeatureFolderOptions.AreaFolderName if using Areas) cannot be in your project namespace.

Using areas

If you want to enable areas, there are two pieces of code to add:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc()
                .AddFeatureFolders()
                .AddAreaFeatureFolders();
    
        // "Features" is the default feature folder root. To override, pass along 
        // a new FeatureFolderOptions object with a different FeatureFolderName
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseDeveloperExceptionPage();
        app.UseMvcWithDefaultRoute().UseMvc(routes => 
            routes.MapRoute(
                name: "areaRoute",
                template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"));
    }

The first piece is to add the .AddAreaFeaturesFolders() after the .AddFeatureFolders(). This adds the view locations for the areas. The second part is another .UseMvc() method to configure the route to area controllers.

Now areas can be added using the default area layout combined with the feature folder setup. Example:

/Areas
    /Administration // this is the area name
        /Overview   // this is the controller name
            /OverviewController.cs
            /Index.cshtml

If ReSharper (or Rider) is being annoying

Then add the package JetBrains.Annotations to the web app project and add the following lines above the Startup class between the using statements and the namespace.

[assembly: AspMvcViewLocationFormat(@"~\Features\{1}\{0}.cshtml")]
[assembly: AspMvcViewLocationFormat(@"~\Features\{0}.cshtml")]
[assembly: AspMvcViewLocationFormat(@"~\Features\Shared\{0}.cshtml")]

[assembly: AspMvcAreaViewLocationFormat(@"~\Areas\{2}\{1}\{0}.cshtml")]
[assembly: AspMvcAreaViewLocationFormat(@"~\Areas\{2}\Features\{1}\{0}.cshtml")]
[assembly: AspMvcAreaViewLocationFormat(@"~\Areas\{2}\{0}.cshtml")]
[assembly: AspMvcAreaViewLocationFormat(@"~\Areas\{2}\Shared\{0}.cshtml")]

Replace 'Features' and 'Areas' part if you set a custom folder name.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 140 8/8/2024
1.0.0 104 8/8/2024