├── .gitignore ├── AddFeatureFolders.sln ├── LICENSE ├── README.md ├── appveyor.yml ├── build.ps1 ├── sample └── WebApplication │ ├── Areas │ ├── Administration │ │ ├── Features │ │ │ └── Test │ │ │ │ ├── Index.cshtml │ │ │ │ ├── SubFolder │ │ │ │ ├── Index.cshtml │ │ │ │ └── SubFolderController.cs │ │ │ │ └── TestController.cs │ │ └── Overview │ │ │ ├── Index.cshtml │ │ │ └── OverviewController.cs │ └── _ViewImports.cshtml │ ├── Features │ ├── Bar │ │ ├── Home.cshtml │ │ └── HomeController.cs │ ├── Foo │ │ ├── Home.cshtml │ │ ├── HomeController.cs │ │ └── SubFoo │ │ │ ├── SubFoo.cshtml │ │ │ └── SubFooController.cs │ ├── Home │ │ ├── Home.cshtml │ │ └── HomeController.cs │ ├── Shared │ │ ├── Components │ │ │ └── MainMenu │ │ │ │ ├── Default.cshtml │ │ │ │ └── MainMenuViewComponent.cs │ │ └── _Layout.cshtml │ ├── _ViewImports.cshtml │ └── _ViewStart.cshtml │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── Startup.cs │ ├── WebApplication.csproj │ └── web.config ├── src └── OdeToCode.AddFeatureFolders │ ├── AreaFeatureFolderOptions.cs │ ├── FeatureControllerModelConvention.cs │ ├── FeatureFolderOptions.cs │ ├── FeatureViewLocationExpander.cs │ ├── OdeToCode.AddFeatureFolders.csproj │ └── ServiceCollectionExtensions.cs └── test └── OdeToCode.AddFeatureFolders.Tests ├── OdeToCode.AddFeatureFolders.Tests.csproj └── Properties └── FeatureControllerModelConventionTests.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /AddFeatureFolders.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio 15 3 | VisualStudioVersion = 15.0.26430.6 4 | MinimumVisualStudioVersion = 15.0.26124.0 5 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{6C8D0F52-A4B8-43C2-8086-2C1591DF47DA}" 6 | EndProject 7 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OdeToCode.AddFeatureFolders", "src\OdeToCode.AddFeatureFolders\OdeToCode.AddFeatureFolders.csproj", "{3075800F-560F-4787-964E-484648201CDB}" 8 | EndProject 9 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{41A0898F-34AC-4A6F-9364-5BA432412C14}" 10 | EndProject 11 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OdeToCode.AddFeatureFolders.Tests", "test\OdeToCode.AddFeatureFolders.Tests\OdeToCode.AddFeatureFolders.Tests.csproj", "{1108FCFF-072D-4B7A-A37F-7F1304821B73}" 12 | EndProject 13 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{D3923ABE-3B29-469A-A6CD-0845029AA539}" 14 | EndProject 15 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication", "sample\WebApplication\WebApplication.csproj", "{5EC27CA4-E5FB-46F9-8047-8214FF7ACEDF}" 16 | EndProject 17 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B55FC53B-F6B3-4504-B27F-3AFB6F77A4ED}" 18 | ProjectSection(SolutionItems) = preProject 19 | .gitignore = .gitignore 20 | appveyor.yml = appveyor.yml 21 | build.ps1 = build.ps1 22 | LICENSE = LICENSE 23 | README.md = README.md 24 | EndProjectSection 25 | EndProject 26 | Global 27 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 28 | Debug|Any CPU = Debug|Any CPU 29 | Release|Any CPU = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 32 | {3075800F-560F-4787-964E-484648201CDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {3075800F-560F-4787-964E-484648201CDB}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {3075800F-560F-4787-964E-484648201CDB}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {3075800F-560F-4787-964E-484648201CDB}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {1108FCFF-072D-4B7A-A37F-7F1304821B73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {1108FCFF-072D-4B7A-A37F-7F1304821B73}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {1108FCFF-072D-4B7A-A37F-7F1304821B73}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {1108FCFF-072D-4B7A-A37F-7F1304821B73}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {5EC27CA4-E5FB-46F9-8047-8214FF7ACEDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {5EC27CA4-E5FB-46F9-8047-8214FF7ACEDF}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {5EC27CA4-E5FB-46F9-8047-8214FF7ACEDF}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {5EC27CA4-E5FB-46F9-8047-8214FF7ACEDF}.Release|Any CPU.Build.0 = Release|Any CPU 44 | EndGlobalSection 45 | GlobalSection(SolutionProperties) = preSolution 46 | HideSolutionNode = FALSE 47 | EndGlobalSection 48 | GlobalSection(NestedProjects) = preSolution 49 | {3075800F-560F-4787-964E-484648201CDB} = {6C8D0F52-A4B8-43C2-8086-2C1591DF47DA} 50 | {1108FCFF-072D-4B7A-A37F-7F1304821B73} = {41A0898F-34AC-4A6F-9364-5BA432412C14} 51 | {5EC27CA4-E5FB-46F9-8047-8214FF7ACEDF} = {D3923ABE-3B29-469A-A6CD-0845029AA539} 52 | EndGlobalSection 53 | EndGlobal 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 K. Scott Allen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AddFeatureFolders 2 | [![Build status](https://ci.appveyor.com/api/projects/status/k4aotmbkugavs2mq?svg=true)](https://ci.appveyor.com/project/OdeToCode/addfeaturefolders) 3 | ### Installation 4 | ``` 5 | Install-Package OdeToCode.AddFeatureFolders 6 | ``` 7 | 8 | ### Usage 9 | ```c# 10 | public class Startup 11 | { 12 | public void ConfigureServices(IServiceCollection services) 13 | { 14 | services.AddMvc() 15 | .AddFeatureFolders(); 16 | 17 | // "Features" is the default feature folder root. To override, pass along 18 | // a new FeatureFolderOptions object with a different FeatureFolderName 19 | } 20 | 21 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 22 | { 23 | app.UseDeveloperExceptionPage(); 24 | app.UseMvcWithDefaultRoute(); 25 | } 26 | } 27 | ``` 28 | 29 | ### Now you can organize controllers and views in a Features folder hierarchy 30 | 31 | See the sample folder for more examples. 32 | 33 | \Features 34 | \Home 35 | \HomeController.cs 36 | \HomeViewModel.cs 37 | \HomeIndexHandler.cs 38 | \HomeIndexQuery.cs 39 | \Index.cshtml 40 | 41 | 42 | ### Important! 43 | AddFeatureFolders **uses the namespace of the controller to figure out where the views are**. 44 | For example: 45 | ``` 46 | /Features 47 | /Robots 48 | /Robots.cshtml 49 | ``` 50 | The above example folder structure relies on the namespace of the controller being `.Features.Robots`. 51 | 52 | If you encounter problems with MVC locating the views, check your controller namespace. 53 | 54 | #### Disclaimer 55 | 56 | Your feature folder name (`FeatureFolderOptions.FeatureFolderName` or `AreaFeatureFolderOptions.AreaFolderName` if using Areas) cannot be in your project namespace. 57 | 58 | See: [Issue #27](https://github.com/OdeToCode/AddFeatureFolders/issues/27) 59 | 60 | ### Using areas 61 | 62 | If you want to enable areas, there are two pieces of code to add: 63 | ```c# 64 | public void ConfigureServices(IServiceCollection services) 65 | { 66 | services.AddMvc() 67 | .AddFeatureFolders() 68 | .AddAreaFeatureFolders(); 69 | 70 | // "Features" is the default feature folder root. To override, pass along 71 | // a new FeatureFolderOptions object with a different FeatureFolderName 72 | } 73 | 74 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 75 | { 76 | app.UseDeveloperExceptionPage(); 77 | app.UseMvcWithDefaultRoute().UseMvc(routes => 78 | routes.MapRoute( 79 | name: "areaRoute", 80 | template: "{area:exists}/{controller=Home}/{action=Index}/{id?}")); 81 | } 82 | ``` 83 | 84 | The first piece is to add the ```.AddAreaFeaturesFolders()``` after the ```.AddFeatureFolders()```. 85 | This adds the view locations for the areas. 86 | The second part is another ```.UseMvc()``` method to configure the route to area controllers. 87 | 88 | Now areas can be added using the default area layout combined with the feature folder setup. 89 | Example: 90 | ``` 91 | /Areas 92 | /Administration // this is the area name 93 | /Overview // this is the controller name 94 | /OverviewController.cs 95 | /Index.cshtml 96 | ``` 97 | 98 | ### If ReSharper (or Rider) is being annoying 99 | Then add the package ```JetBrains.Annotations``` to the web app project and add the following lines 100 | above the Startup class between the using statements and the namespace. 101 | ```c# 102 | [assembly: AspMvcViewLocationFormat(@"~\Features\{1}\{0}.cshtml")] 103 | [assembly: AspMvcViewLocationFormat(@"~\Features\{0}.cshtml")] 104 | [assembly: AspMvcViewLocationFormat(@"~\Features\Shared\{0}.cshtml")] 105 | 106 | [assembly: AspMvcAreaViewLocationFormat(@"~\Areas\{2}\{1}\{0}.cshtml")] 107 | [assembly: AspMvcAreaViewLocationFormat(@"~\Areas\{2}\Features\{1}\{0}.cshtml")] 108 | [assembly: AspMvcAreaViewLocationFormat(@"~\Areas\{2}\{0}.cshtml")] 109 | [assembly: AspMvcAreaViewLocationFormat(@"~\Areas\{2}\Shared\{0}.cshtml")] 110 | ``` 111 | Replace 'Features' and 'Areas' part if you set a custom folder name. 112 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.{build} 2 | skip_non_tags: false 3 | build_script: 4 | - ps: .\build.ps1 5 | test: off 6 | branches: 7 | only: 8 | - master 9 | artifacts: 10 | - path: .\artifacts\**\*.nupkg 11 | name: NuGet 12 | deploy: 13 | - provider: NuGet 14 | api_key: 15 | secure: tg20msJwxYqlk0EwjZ1MuPXsX+ORK/7fxdeopd1aSnPaJX+gq9srs7i7VINiYTWS 16 | skip_symbols: true 17 | on: 18 | branch: master 19 | appveyor_repo_tag: true 20 | notifications: 21 | - provider: Email 22 | to: 23 | - scott@odetocode.com 24 | subject: AddFeatureFolders Build 25 | on_build_success: true 26 | on_build_failure: true 27 | on_build_status_changed: false -------------------------------------------------------------------------------- /build.ps1: -------------------------------------------------------------------------------- 1 | function Exec 2 | { 3 | [CmdletBinding()] param( 4 | [Parameter(Position=0,Mandatory=1)][scriptblock]$cmd, 5 | [Parameter(Position=1,Mandatory=0)][string]$errorMessage = ($msgs.error_bad_command -f $cmd) 6 | ) 7 | & $cmd 8 | if ($lastexitcode -ne 0) { 9 | throw ("Exec: " + $errorMessage) 10 | } 11 | } 12 | 13 | if(Test-Path .\artifacts) { Remove-Item .\artifacts -Force -Recurse } 14 | 15 | exec { & dotnet restore } 16 | 17 | exec { & dotnet build .\src\OdeToCode.AddFeatureFolders -c Release } 18 | 19 | pushd .\test\OdeToCode.AddFeatureFolders.Tests\ 20 | exec { & dotnet test -c Release } 21 | popd 22 | 23 | exec { & dotnet pack .\src\OdeToCode.AddFeatureFolders -c Release -o ..\..\artifacts } 24 | -------------------------------------------------------------------------------- /sample/WebApplication/Areas/Administration/Features/Test/Index.cshtml: -------------------------------------------------------------------------------- 1 | @model Type 2 | 3 | @{ 4 | Layout = "_Layout"; 5 | } 6 | 7 |
8 | Main content rendered by @Model.FullName 9 |
10 | -------------------------------------------------------------------------------- /sample/WebApplication/Areas/Administration/Features/Test/SubFolder/Index.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "_Layout"; 3 | } 4 | 5 |
6 | Main component rendered by Area Administration::SubFolder::SubFolderController::Index 7 |
8 | -------------------------------------------------------------------------------- /sample/WebApplication/Areas/Administration/Features/Test/SubFolder/SubFolderController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace WebApplication.Areas.Administration.Features.Test.SubFolder 4 | { 5 | [Area("Administration")] 6 | public class SubFolderController : Controller 7 | { 8 | [HttpGet] 9 | public IActionResult Index() 10 | { 11 | return View(); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /sample/WebApplication/Areas/Administration/Features/Test/TestController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace WebApplication.Areas.Administration.Features.Test 4 | { 5 | [Area("Administration")] 6 | public class TestController : Controller 7 | { 8 | [HttpGet] 9 | public IActionResult Index() 10 | { 11 | return View("Index", typeof(TestController)); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /sample/WebApplication/Areas/Administration/Overview/Index.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "_Layout"; 3 | } 4 |
5 | Main component rendered by Area Administration::OverviewController::Index 6 | Test 7 |
8 | -------------------------------------------------------------------------------- /sample/WebApplication/Areas/Administration/Overview/OverviewController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace WebApplication.Areas.Administration.Overview 4 | { 5 | [Area("Administration")] 6 | public class OverviewController : Controller 7 | { 8 | [HttpGet] 9 | public IActionResult Index() 10 | { 11 | return View(); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /sample/WebApplication/Areas/_ViewImports.cshtml: -------------------------------------------------------------------------------- 1 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -------------------------------------------------------------------------------- /sample/WebApplication/Features/Bar/Home.cshtml: -------------------------------------------------------------------------------- 1 | 
2 | Main content rendered by FEATURE:Bar:HomeController::Home 3 |
-------------------------------------------------------------------------------- /sample/WebApplication/Features/Bar/HomeController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace WebApplication.Features.Bar 4 | { 5 | [Route("Bar/[controller]")] 6 | public class HomeController : Controller 7 | { 8 | public IActionResult Home() 9 | { 10 | return View(); 11 | } 12 | 13 | [HttpPost] 14 | public IActionResult Search() 15 | { 16 | // do some stuff... 17 | return View("Home"); 18 | // this will also work 19 | // return Redirect("/Bar/Home"); 20 | 21 | // TODO: posting to /Foo/Home or /Bar/Home with Content-Type: application/x-www-form-urlencoded always routes to the "Bar" feature controller 22 | // return RedirectToAction("Home"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /sample/WebApplication/Features/Foo/Home.cshtml: -------------------------------------------------------------------------------- 1 | 
2 | Main content rendered by FEATURE:Foo:HomeController::Home 3 |
-------------------------------------------------------------------------------- /sample/WebApplication/Features/Foo/HomeController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace WebApplication.Features.Foo 4 | { 5 | [Route("Foo/[controller]")] 6 | public class HomeController : Controller 7 | { 8 | public IActionResult Home() 9 | { 10 | return View(); 11 | } 12 | 13 | [HttpPost] 14 | public IActionResult Search() 15 | { 16 | // do some stuff... 17 | return View("Home"); 18 | // this will also work 19 | // return Redirect("/Foo/Home"); 20 | 21 | // TODO: posting to /Foo/Home or /Bar/Home with Content-Type: application/x-www-form-urlencoded always routes to the "Bar" feature controller 22 | // return RedirectToAction("Home"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /sample/WebApplication/Features/Foo/SubFoo/SubFoo.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "_Layout"; 3 | } 4 | 5 |
6 | Main content rendered by FEATURE:Foo:SubFoo:SubFooController::SubFoo 7 |
8 | -------------------------------------------------------------------------------- /sample/WebApplication/Features/Foo/SubFoo/SubFooController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace WebApplication.Features.Foo.SubFoo 4 | { 5 | [Route("Foo/SubFoo/[controller]")] 6 | public class SubFooController : Controller 7 | { 8 | [HttpGet] 9 | public IActionResult SubFoo() 10 | { 11 | return View(); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /sample/WebApplication/Features/Home/Home.cshtml: -------------------------------------------------------------------------------- 1 | 
2 | Main content rendered by HomeController::Home 3 | Bar Home 4 | @Assembly.GetExecutingAssembly().FullName 5 |
-------------------------------------------------------------------------------- /sample/WebApplication/Features/Home/HomeController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace WebApplication.Features.Home 4 | { 5 | [Route("")] 6 | public class HomeController : Controller 7 | { 8 | public IActionResult Home() 9 | { 10 | return View(); 11 | } 12 | 13 | [Route("[action]")] 14 | public IActionResult Create() 15 | { 16 | return Content("Create"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /sample/WebApplication/Features/Shared/Components/MainMenu/Default.cshtml: -------------------------------------------------------------------------------- 1 |  2 | -------------------------------------------------------------------------------- /sample/WebApplication/Features/Shared/Components/MainMenu/MainMenuViewComponent.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace WebApplication.Features.Shared.Components.MainMenu 4 | { 5 | public class MainMenuViewComponent : ViewComponent 6 | { 7 | public IViewComponentResult Invoke() 8 | { 9 | return View(); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /sample/WebApplication/Features/Shared/_Layout.cshtml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | @ViewBag.Title 7 | 8 | 9 | @await Component.InvokeAsync("MainMenu") 10 | @RenderBody() 11 | 37 | 38 | -------------------------------------------------------------------------------- /sample/WebApplication/Features/_ViewImports.cshtml: -------------------------------------------------------------------------------- 1 | @using System.Reflection 2 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -------------------------------------------------------------------------------- /sample/WebApplication/Features/_ViewStart.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "_Layout"; 3 | } 4 | -------------------------------------------------------------------------------- /sample/WebApplication/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace WebApplication 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | var host = WebHost.CreateDefaultBuilder() 11 | .UseStartup() 12 | .Build(); 13 | 14 | host.Run(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/WebApplication/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:8557/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "WebApplication": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "launchUrl": "http://localhost:5000", 22 | "environmentVariables": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /sample/WebApplication/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using Microsoft.Extensions.Logging; 5 | using OdeToCode.AddFeatureFolders; 6 | 7 | namespace WebApplication 8 | { 9 | public class Startup 10 | { 11 | public void ConfigureServices(IServiceCollection services) 12 | { 13 | services.AddMvc() 14 | .AddFeatureFolders() 15 | .AddAreaFeatureFolders(); 16 | 17 | // "Features" is the default feature folder root. To override, pass along 18 | // a new FeatureFolderOptions object with a different FeatureFolderName 19 | } 20 | 21 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) 22 | { 23 | app.UseDeveloperExceptionPage(); 24 | app.UseRouting(); 25 | app.UseEndpoints(e => { 26 | e.MapControllerRoute(name:"areaRoute", pattern:"{area:exists}/{controller=Home}/{action=Index}/{id?}"); 27 | }); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /sample/WebApplication/WebApplication.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | netcoreapp3.0 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | $(IncludeRazorContentInPack) 13 | 14 | 15 | -------------------------------------------------------------------------------- /sample/WebApplication/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/OdeToCode.AddFeatureFolders/AreaFeatureFolderOptions.cs: -------------------------------------------------------------------------------- 1 | namespace OdeToCode.AddFeatureFolders 2 | { 3 | public class AreaFeatureFolderOptions 4 | { 5 | /// 6 | /// Options to control the area feature folders 7 | /// 8 | public AreaFeatureFolderOptions() : this(new FeatureFolderOptions()) 9 | { 10 | } 11 | 12 | /// 13 | /// Options to control the area feature folders 14 | /// 15 | /// Can read options from if non default options are chosen 16 | public AreaFeatureFolderOptions(FeatureFolderOptions options) 17 | { 18 | AreaFolderName = "Areas"; 19 | DefaultAreaViewLocation = $@"Areas\{{2}}\{options.FeatureNamePlaceholder}\{{0}}.cshtml"; 20 | FeatureFolderName = options.FeatureFolderName; 21 | } 22 | 23 | /// 24 | /// The name of the root area folder on disk (default: 'Areas') 25 | /// 26 | public string AreaFolderName { get; set; } 27 | 28 | /// 29 | /// The default view location for areas. Helps intellisense find razor views. Example: 30 | /// "\Areas\{2}\{Feature}\{0}.cshtml". 31 | /// Razor replaces the area name into {2} placeholder, the controller name into {1} placeholder & view name into the {0} placeholder. 32 | /// The {Feature} placeholder must be the same name as the FeatureNamePlaceholder to replace 33 | /// the placeholder with the path to the feature folder. 34 | /// 35 | public string DefaultAreaViewLocation { get; set; } 36 | 37 | /// 38 | /// The name of the root feature folder on disk (default: 'Features') 39 | /// Only change this if there is a custom feature folder name set in the feature folder options. 40 | /// 41 | public string FeatureFolderName { get; set; } 42 | } 43 | } -------------------------------------------------------------------------------- /src/OdeToCode.AddFeatureFolders/FeatureControllerModelConvention.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Linq; 4 | using Microsoft.AspNetCore.Mvc.ApplicationModels; 5 | 6 | namespace OdeToCode.AddFeatureFolders 7 | { 8 | public class FeatureControllerModelConvention : IControllerModelConvention 9 | { 10 | private readonly string _folderName; 11 | private readonly Func _nameDerivationStrategy; 12 | 13 | public FeatureControllerModelConvention(FeatureFolderOptions options) 14 | { 15 | if(options == null) 16 | throw new ArgumentNullException(nameof(options)); 17 | 18 | _folderName = options.FeatureFolderName; 19 | _nameDerivationStrategy = options.DeriveFeatureFolderName ?? DeriveFeatureFolderName; 20 | } 21 | 22 | public void Apply(ControllerModel model) 23 | { 24 | if(model == null) 25 | throw new ArgumentNullException(nameof(model)); 26 | 27 | var featureName = _nameDerivationStrategy(model); 28 | model.Properties.Add("feature", featureName); 29 | } 30 | 31 | private string DeriveFeatureFolderName(ControllerModel model) 32 | { 33 | var @namespace = model.ControllerType.Namespace; 34 | var result = @namespace.Split('.') 35 | .SkipWhile(s => s != _folderName) 36 | .Aggregate("", Path.Combine); 37 | 38 | return result; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/OdeToCode.AddFeatureFolders/FeatureFolderOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.AspNetCore.Mvc.ApplicationModels; 3 | 4 | namespace OdeToCode.AddFeatureFolders 5 | { 6 | /// 7 | /// Options to control the behavior of feature folders 8 | /// 9 | public class FeatureFolderOptions 10 | { 11 | public FeatureFolderOptions() 12 | { 13 | FeatureFolderName = "Features"; 14 | DeriveFeatureFolderName = null; 15 | FeatureNamePlaceholder = "{Feature}"; 16 | DefaultViewLocation = @"\Features\{0}\{1}.cshtml"; 17 | } 18 | 19 | /// 20 | /// The name of the root feature folder on disk (default: 'Features') 21 | /// 22 | public string FeatureFolderName { get; set; } 23 | 24 | /// 25 | /// Given a ControllerModel object, returns the path to the feature folder. 26 | /// Only set this property if you want to override the default logic. 27 | /// The default logic takes the namespace of a Controller and assumes the 28 | /// namespace maps to a folder. Examples: 29 | /// Project.Name.Features.Admin.ManageUsers -> Features\Admin\ManageUsers 30 | /// Project.Name.Features.Admin -> Features\Admin 31 | /// Note the name "Features" is set by the FeatureFolderName property. 32 | /// 33 | public Func DeriveFeatureFolderName { get; set; } 34 | 35 | 36 | /// 37 | /// Used internally in RazorOptions.ViewLocationFormats strings. The Default is {Feature}, 38 | /// so the first format string in Razor options will be {Feature}\{0}.cshtml. Razor places 39 | /// the view name into the {0} placeholder, the FeatureViewLocationExander class in this project 40 | /// replaces {Feature} with the feature path derived from the ControllerModel 41 | /// 42 | public string FeatureNamePlaceholder { get; set; } 43 | 44 | /// 45 | /// The default view location. Helps intellisense find razor views. Example: 46 | /// "\Features\{0}\{1}.cshtml". 47 | /// Razor replaces the controller name into {0} placeholder & view name into the {1} placeholder. 48 | /// 49 | public string DefaultViewLocation { get; set; } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/OdeToCode.AddFeatureFolders/FeatureViewLocationExpander.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Microsoft.AspNetCore.Mvc.Controllers; 4 | using Microsoft.AspNetCore.Mvc.Razor; 5 | 6 | namespace OdeToCode.AddFeatureFolders 7 | { 8 | public class FeatureViewLocationExpander : IViewLocationExpander 9 | { 10 | private readonly string _placeholder; 11 | 12 | public FeatureViewLocationExpander(FeatureFolderOptions options) 13 | { 14 | _placeholder = options.FeatureNamePlaceholder; 15 | } 16 | 17 | public void PopulateValues(ViewLocationExpanderContext context) 18 | { 19 | // see: https://stackoverflow.com/questions/36802661/what-is-iviewlocationexpander-populatevalues-for-in-asp-net-core-mvc 20 | context.Values["action_displayname"] = context.ActionContext.ActionDescriptor.DisplayName; 21 | } 22 | 23 | public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable viewLocations) 24 | { 25 | if(context == null) 26 | throw new ArgumentNullException(nameof(context)); 27 | 28 | if(viewLocations == null) 29 | throw new ArgumentNullException(nameof(viewLocations)); 30 | 31 | var controllerDescriptor = context.ActionContext.ActionDescriptor as ControllerActionDescriptor; 32 | var featureName = controllerDescriptor?.Properties["feature"] as string; 33 | 34 | foreach (var location in viewLocations) 35 | { 36 | yield return location.Replace(_placeholder, featureName); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/OdeToCode.AddFeatureFolders/OdeToCode.AddFeatureFolders.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Use feature folders to organize controllers and views in ASP.NET Core 5 | OdeToCode LLC 6 | 2.0.3 7 | K. Scott Allen 8 | netstandard1.6;netstandard2.0;net452 9 | OdeToCode.AddFeatureFolders 10 | OdeToCode.AddFeatureFolders 11 | feature folders;asp.net core 12 | https://github.com/OdeToCode/AddFeatureFolders/ 13 | https://raw.githubusercontent.com/OdeToCode/AddFeatureFolders/master/LICENSE 14 | git 15 | https://raw.githubusercontent.com/OdeToCode/AddFeatureFolders/ 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/OdeToCode.AddFeatureFolders/ServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using OdeToCode.AddFeatureFolders; 3 | 4 | // ReSharper disable once CheckNamespace 5 | 6 | namespace Microsoft.Extensions.DependencyInjection 7 | { 8 | public static class ServiceCollectionExtensions 9 | { 10 | /// 11 | /// Use feature folders with custom options 12 | /// 13 | public static IMvcBuilder AddFeatureFolders(this IMvcBuilder services, FeatureFolderOptions options) 14 | { 15 | if (services == null) 16 | throw new ArgumentNullException(nameof(services)); 17 | 18 | if (options == null) 19 | throw new ArgumentException(nameof(options)); 20 | 21 | var expander = new FeatureViewLocationExpander(options); 22 | 23 | services.AddMvcOptions(o => o.Conventions.Add(new FeatureControllerModelConvention(options))) 24 | .AddRazorOptions(o => 25 | { 26 | o.ViewLocationFormats.Clear(); 27 | o.ViewLocationFormats.Add(options.FeatureNamePlaceholder + @"\{0}.cshtml"); 28 | o.ViewLocationFormats.Add(options.FeatureFolderName + @"\Shared\{0}.cshtml"); 29 | o.ViewLocationFormats.Add(options.DefaultViewLocation); 30 | 31 | o.ViewLocationExpanders.Add(expander); 32 | }); 33 | 34 | return services; 35 | } 36 | 37 | /// 38 | /// Use areas with feature folders and custom options 39 | /// 40 | public static IMvcBuilder AddAreaFeatureFolders(this IMvcBuilder services, AreaFeatureFolderOptions options) 41 | { 42 | services.AddRazorOptions(o => 43 | { 44 | o.AreaViewLocationFormats.Clear(); 45 | o.AreaViewLocationFormats.Add(options.DefaultAreaViewLocation); 46 | o.AreaViewLocationFormats.Add(options.AreaFolderName + @"\{2}\{1}\{0}.cshtml"); 47 | o.AreaViewLocationFormats.Add(options.AreaFolderName + @"\{2}\Shared\{0}.cshtml"); 48 | o.AreaViewLocationFormats.Add(options.AreaFolderName + @"\Shared\{0}.cshtml"); 49 | o.AreaViewLocationFormats.Add(options.FeatureFolderName + @"\Shared\{0}.cshtml"); 50 | }); 51 | 52 | return services; 53 | } 54 | 55 | /// 56 | /// Use feature folders with the default options. Controllers and view will be located 57 | /// under a folder named Features. Shared views are located in Features\Shared. 58 | /// 59 | public static IMvcBuilder AddFeatureFolders(this IMvcBuilder services) 60 | { 61 | return AddFeatureFolders(services, new FeatureFolderOptions()); 62 | } 63 | 64 | /// 65 | /// Use areas with feature folders with the default options. Controllers and views will 66 | /// be located under a folder named Areas with an area specific folder. Shared views are 67 | /// located in Areas\Shared and then in Features\Shared 68 | /// 69 | public static IMvcBuilder AddAreaFeatureFolders(this IMvcBuilder services) 70 | { 71 | return AddAreaFeatureFolders(services, new AreaFeatureFolderOptions()); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /test/OdeToCode.AddFeatureFolders.Tests/OdeToCode.AddFeatureFolders.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net452;netcoreapp1.1;netcoreapp2.0 5 | OdeToCode.AddFeatureFolders.Tests 6 | OdeToCode.AddFeatureFolders.Tests 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /test/OdeToCode.AddFeatureFolders.Tests/Properties/FeatureControllerModelConventionTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Microsoft.AspNetCore.Mvc.ApplicationModels; 4 | using Xunit; 5 | using System.Reflection; 6 | using Controllers; 7 | using Project.Name.Features; 8 | using Project.Name.Features.Admin.ManageGolfers; 9 | using Project.Name.Features.Foo.Bar.Baz; 10 | using static Xunit.Assert; 11 | 12 | namespace OdeToCode.AddFeatureFolders.Tests 13 | { 14 | public class FeatureControllerModelConventionTests 15 | { 16 | [Theory] 17 | [InlineData(typeof(ManageGolfersController), @"Features\Admin\ManageGolfers")] 18 | [InlineData(typeof(ManageUsersController), @"Features\Foo\Bar\Baz")] 19 | [InlineData(typeof(HomeController), @"Features")] 20 | [InlineData(typeof(AboutController), @"")] 21 | public void CanBuildPathFromControllerNamespace(Type controller, string expected) 22 | { 23 | var options = new FeatureFolderOptions(); 24 | var service = new FeatureControllerModelConvention(options); 25 | var controllerType = controller.GetTypeInfo(); 26 | var attributes = new List(); 27 | var model = new ControllerModel(controllerType, attributes); 28 | 29 | service.Apply(model); 30 | 31 | Equal(expected, model.Properties["feature"]); 32 | } 33 | 34 | [Fact] 35 | public void CanUseCustomDerivationStrategy() 36 | { 37 | var options = new FeatureFolderOptions() 38 | { 39 | DeriveFeatureFolderName = c => @"Features\Foo" 40 | }; 41 | var service = new FeatureControllerModelConvention(options); 42 | var controllerType = typeof(ManageUsersController).GetTypeInfo(); 43 | var attributes = new List(); 44 | var model = new ControllerModel(controllerType, attributes); 45 | 46 | service.Apply(model); 47 | 48 | Equal(@"Features\Foo", model.Properties["feature"]); 49 | } 50 | } 51 | } 52 | 53 | namespace Project.Name.Features.Admin.ManageGolfers 54 | { 55 | internal class ManageGolfersController 56 | { 57 | } 58 | } 59 | 60 | namespace Project.Name.Features.Foo.Bar.Baz 61 | { 62 | internal class ManageUsersController 63 | { 64 | } 65 | } 66 | 67 | namespace Project.Name.Features 68 | { 69 | internal class HomeController 70 | { 71 | } 72 | } 73 | 74 | namespace Controllers 75 | { 76 | internal class AboutController 77 | { 78 | 79 | } 80 | } 81 | --------------------------------------------------------------------------------