odetocode / addfeaturefolders Goto Github PK
View Code? Open in Web Editor NEWEnable feature folders for MVC controllers and views in ASP.NET Core
License: MIT License
Enable feature folders for MVC controllers and views in ASP.NET Core
License: MIT License
I am getting the following error
InvalidOperationException: The layout view '/Views/Shared/_Layout.cshtml' could not be located. The following locations were searched:
/Views/Shared/_Layout.cshtml
when I browse to the Login page in a new MVC Core app
The folder structure looks like this:
I've added the following in ConfigureServices
services
.AddMvc()
.AddFeatureFolders()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
and in Configure
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Can anyone help with this please? I'm not sure what I'm doing wrong here
I tried to add a Views folder within my Feature folder but the view cannot be located.
Looking at the source code i understand that it is not currently possible.
What i'm looking for is this setup:
Features -> FeatureFoo -> Views
I'm only asking for it because moving the Account and Manage Views of the default VS MVC template with Individual Auth looks messy.
Please consider it. Thank you.
Adding a second controller action throw an exception
public IActionResult Create()
{
return View();
}
An unhandled exception occurred while processing the request.
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
WebApplication.Features.Foo.HomeController.Home (WebApplication)
WebApplication.Features.Foo.HomeController.Create (WebApplication)
Microsoft.AspNetCore.Mvc.Internal.ActionSelector.SelectBestCandidate(RouteContext context, IReadOnlyList candidates)
Hi @OdeToCode ,
Did you encounter a bug with the FeaturesFolder with .Net Core 2.0 Preview 1? I was working without issues on .Net Core 1.1 with your routine and the views were properly discovered, but now starting with an empty template in preview 1 and adding a home/index view with a controller and attribute routing and a shared layout view gets the latter not displayed by a browser. The only way to have it read is to invoke Layout = "_Layout";
directly from Home Index view.
Today I learned about view components and they seem to have a strict unconfigurable convention for finding component views.
Is there are a cleaner way to use view components with feature folders other than to explicitly specify the full view path when returning a view from a component?
Hi @OdeToCode ,
It seems to be not working with resource files, is that expected?
I created a new ASP.NET Core 2.1 MVC project and named the project ASPNETCoreMVC.Features.Web
.
I added <PackageReference Include="OdeToCode.AddFeatureFolders" Version="2.0.1" />
to my .csproj
.
When I run the app it says
An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'Index' was not found. The following locations were searched:
Features\Web\Features\Home\Index.cshtml
Features\Shared\Index.cshtml
\Features\Index\Home.cshtml
In this case I'd need to change FeatureFolderOptions.FeatureFolderName
to something other than Features
because
@namespace.Split('.')
.SkipWhile(s => s != _folderName)
.Aggregate("", Path.Combine);
is going to stop too early and create the wrong folder path.
Here is an ugly solution
.AddFeatureFolders(new FeatureFolderOptions
{
FeatureFolderName = "Features",
DeriveFeatureFolderName = model =>
{
string assemblyNamespacePrefix = $"{typeof(Startup).Namespace}.";
string @namespace = model
.ControllerType
.Namespace
.Remove(0, assemblyNamespacePrefix.Length);
string result = @namespace.Split('.')
.SkipWhile(s => s != "Features")
.Aggregate("", Path.Combine);
return result;
}
});
But maybe the best thing to do is add a disclaimer to the README.md
that your feature folder name cannot be in the project namespace?
Microsoft Visual Studio Enterprise 2017
Version 15.1 (26403.7) Release
In the controllers, Resharper highlights "View()" as red and says "Cannot resolve View 'ViewName'.
I target ".NETCoreApp 1.1" while AddFeatureFolders project targets "v4.5.2". Resharper doesn't have any issues when I open and run AddFeatureFolders.
Everything else works except that Resharper can't find views. I know this is a Resharper specific issue but I saw that you used Resharper inspection comments and that you might already have a fix for it.
After reading the article you referenced by Steve Smith, I noticed he has a section on side by side support where you can still allow the traditional folder structure or areas support.
I thought it would be cool to add some options so that you can enable or disable these?
Can this be used with Razor Class Libraries ?
I just watched a YouTube video where you briefly discussed AddFeatureFolders and it piqued my interest.
Can it be used for an API written using .NET Core?
Sorry for my ignorance, I'm just starting to learn .NET Core and MVC/APIs in general.
I think this library should allow using all the possibile choices asp.net core has in term of
MVC service registration
I created a new ASP.Net 8 MVC project and followed the steps in the Microsoft documentation (https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-8.0#css-isolation) to implement CSS isolation but it does not work when used with a Features folder. It does work when using the standard Views folder.
This is how I added the CSS:
The CSS file contains the following:
.someClass {
color: red;
font-weight: 800;
}
h1 {
color: green;
font-weight: 800;
}
and the Index.cshtml file contains:
<div>
<h1 class="someClass">Welcome</h1>
</div>
but the text appears black with none of the styles applied.
and I have the styles added to the layout file as mentioned in the docs:
I think the CSS isolation only works using the Views or Pages folder, so I think adding the views to the Features folder instead is breaking it.
If you know how to configure this to get this to work then I'd recommend adding it to the README. Does anyone else have this issue?
I think a feature folder is not complete with some static content, how do you serve it without copying to wwwroot ? I'm following this approach
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?tabs=aspnetcore2x#serve-static-files
app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")), RequestPath = "/StaticFiles" });
But it only works if the file system path starts with a different char than the request path or it will not allow to have any routing parameter in the request.
I think would be nice to indicate how AddFeaturesFolders()
locates project files using a structure tree (Views, Controllers, Areas, ViewComponents) in Readme file. That way is gonna be clear where to put everything. eg.:
\Features
\Home
\HomeController.cs
\HomeViewModel.cs
\HomeIndexHandler.cs
\HomeIndexQuery.cs
\Index.cshtml
This is from the sample site in the repo
Main content rendered by FEATURE:Foo:SubFoo:SubFooController::SubFoo
Given this path, I would expect the url route to be
http://localhost:8557/Foo/SubFoo
and not
http://localhost:8557/SubFoo/SubFoo
I have resorted to using Routes in my controllers to achieve the desired path but just wondering why it does this?
Hi Scott,
Instead of using @await Component.InvokeAsync
, I used the html friendly <vc></vc>
element and it does not work on the browser and no intellisense (the vc is blue instead of dark green). Other TagHelpers such as asp-action
are properly coloured and intellisensed (if that is a verb......).
It works properly on the ASP.NET template and I used the same tag helper <vc:main-menu></vc:main-menu>.
I had to add @addTagHelper *, WebApplication
in _ViewImports
such that it does not crash, but it does not render.
So I wonder if that is not an issue with your extension?
Feature Request
It would be interesting to add an option that does not clear the ViewLocationFormats
collection.
Sometimes we need to recognize views in feature folders and in the default locations, like Views
and Shared
in the same project.
I upgraded project from .net core 2.2 to 3.0. and FeatureFolders routing is not working with Endpoint routing. Returns 404.
Since app.UseMvcWithDefaultRoute() and app.UseMvc are depreciated whats the way to configure FeatureFolders?
In the sample app coming with this project, I added the following link to the layout file
Go Home
If I run the app and the page is served from one of the feature controllers then the link is correctly generated. If I navigate to any of the Area pages then it does not work. I was wondering if this is due to the fact the the area pages are not p
I dont seem to be able to load my index view without putting index in the url.
for example, i should be able to go to http://localhost:5000/serverRoute and it should load, however it doesnt. I need to check the path to http://localhost:5000/serverRoute/index in order for it to load.
Is this right?
My route is slightly different as i am using SPA, and looks like this
// routes
app.UseMvc(routes =>
{
// default goes to Home, and angular will deal with client side routing
routes.MapRoute(
name: "default",
template: "{*url}",
defaults: new { controller = "home", action = "index" });
});
Help much appreicated
If we configure feature folders, then all views into "feaure" folder dot'n use the _ViewImports.cshtml
If i install this package it is never able to be referenced in my project. I am guessing something in old in this project that is stopping it being referenced.
Shame as would love to use, i have copied your code and put it directly into my project and it works fine....
Any reason this shouldnt work?
I am targeting net461 in my project and referencing 1.1.* packages from aspnetcore.
I've added this package to a core 2.2 MVC project and it's working fine apart from the following issue.
I'm also using Identity and I've customised the default pages which, by default, now reside under Areas/Identity/
These customised pages use the same layout as the rest of the app, which is in Features/Shared/_Layout.cshtml
Also in this shared folder is a partial that's referenced (by name only) from the main layout: Features/Shared/_LoginPartial.cshtml
The app works fine apart from when visiting any of the Identity pages - I get this error:
System.InvalidOperationException: The partial view '_LoginPartial' was not found. The following locations were searched:
/Areas/Identity/Pages/Account/_LoginPartial.cshtml
/Areas/Identity/Pages/_LoginPartial.cshtml
/Areas/Identity/Pages/Shared/_LoginPartial.cshtml
/Areas/Identity/Views/Shared/_LoginPartial.cshtml
/Pages/Shared/_LoginPartial.cshtml
/Views/Shared/_LoginPartial.cshtml
So the Identity pages aren't finding the "shared" layout / partial views.
Any ideas how to solve this?
@OdeToCode This nuget is very helpful. But please explain us how to use "features" together with "areas"?
We use areas, and in each area there are features. Is this nuget capable of handling that case?
If you look at article you linked to in your (awesome) blog, you see it shows this is possible in section "Side-By-Side Feature Folders", but I wonder how to do it with this nuget?
With thanks!
Noticed that in the Sub Folder example, Resharper shows the view as red. I have had this issue in the past for sub folders. Has anyone been able to resolve this (btw, the code shown for Resharper Attributes works great for non-nested feature folders).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.