2

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?

1
  • It is a standard that the Razor engine recognize and developers expect when working with MVC3. Like any standard, you can choose to do otherwise, but this falls in the bad practice side of programming. Standards are neat, they lead to continuity and better readability in the long run.
    – Yan Brunet
    Nov 19, 2012 at 18:26

2 Answers 2

0

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.

0

You may be able to use RouteMap() to change the default actions for each controller so they aren't all using Index, but whatever your want your .cshtml file to be called. That should still leverage the magic without re-inventing the wheel.

Hope this helps...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.