Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/OdeToCode/AddFeatureFolders
Enable feature folders for MVC controllers and views in ASP.NET Core
https://github.com/OdeToCode/AddFeatureFolders
Last synced: about 2 months ago
JSON representation
Enable feature folders for MVC controllers and views in ASP.NET Core
- Host: GitHub
- URL: https://github.com/OdeToCode/AddFeatureFolders
- Owner: OdeToCode
- License: mit
- Created: 2016-10-09T19:30:38.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-10-07T22:33:54.000Z (about 2 years ago)
- Last Synced: 2024-10-04T20:05:54.692Z (2 months ago)
- Language: C#
- Homepage:
- Size: 54.7 KB
- Stars: 250
- Watchers: 14
- Forks: 66
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-dotnet-core - AddFeatureFolders - Enable feature folders for MVC controllers and views in ASP.NET Core. (Frameworks, Libraries and Tools / Application Templates)
- fucking-awesome-dotnet-core - AddFeatureFolders - Enable feature folders for MVC controllers and views in ASP.NET Core. (Frameworks, Libraries and Tools / Application Templates)
- awesome-dotnet-core - AddFeatureFolders - Enable feature folders for MVC controllers and views in ASP.NET Core. (Frameworks, Libraries and Tools / Application Templates)
- awesome-dotnet-core - AddFeatureFolders - 为ASP.NET Core中的MVC控制器和视图启用功能文件夹。 (框架, 库和工具 / 应用程序模板)
README
# AddFeatureFolders
[![Build status](https://ci.appveyor.com/api/projects/status/k4aotmbkugavs2mq?svg=true)](https://ci.appveyor.com/project/OdeToCode/addfeaturefolders)
### Installation
```
Install-Package OdeToCode.AddFeatureFolders
```### Usage
```c#
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 `.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.
See: [Issue #27](https://github.com/OdeToCode/AddFeatureFolders/issues/27)
### Using areas
If you want to enable areas, there are two pieces of code to add:
```c#
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.
```c#
[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.