For controllers Foo, Bar, and Baz, I have a folder structure that looks like this:
Views
Foo
Index.csthml
Bar
Index.csthml
Baz
Index.csthml
_Rarity.cshtml
Must it really be this way? It’s a bit of a pain to determine the right file to edit because most of them are called Index.cshtml. Ideally I’d like something a bit more like this:
Views
Foo.csthml
Bar.csthml
Baz.csthml
Baz
_Rarity.cshtml
I’m aware that I can pass a fully-qualified path to the View() method of the Controller class; i.e. inside of FooController‘s Index() method, I can call this.View("~/Views/Foo.cshtml"). Is this the best I can get?
This fits outside of the nature of the default Razor view engine; it’s a conventional approach to use the first setup as you mentioned above. However, you could build your own convention by customizing the Razor view engine, and the folder it needs to look for views in. However, the problem that you may experience with your design is conflicting file names; For instance, you could make the case that Foo.cshtml maps to Foo controller, Index action, but that means you couldn’t easily map Foo controller Another action method to a view without a special convention for it.
Conclusion: yes, with customizations, but beware of complexity and edge cases.