3

I've been working with ASP.Net MVC (3) for some time now and i like it a lot. But one thing i find a bit annoying is having to browse between the controllers / views / model / script directory all the time. So i'm wondering if there's a way to tell MVC to look for the files in a different location?

Maybe someone can tell me how to simply group the files together by controller like:

Directory: /Membership

  • MembershipController
  • LogOnView
  • LogOnModel
  • RegisterView
  • RegisterModel

Kind regards Olav

t0PPy
  • 311
  • 3
  • 12
  • I think you are interested in MVC Areas: http://msdn.microsoft.com/en-us/library/ee671793.aspx – Kirk Woll Feb 05 '11 at 15:01
  • @Kirk I think even after using MVC areas, you still have controllers / views/ model in each area which he needs to browse. – Mahesh Velaga Feb 05 '11 at 15:02
  • 1
    Install Resharper. To go to a controller or a view, etc (with Idea keybindings) you just press ctrl-N and start typing the name then chose from the list. Your tools make life easier. Unfortunately it doesn't work for scripts :-/ – Sean Feb 05 '11 at 15:10
  • @Sean DPack does the same thing with alt-u. It is free and works with scripts too. – Quesi Feb 05 '11 at 15:50
  • @Quesi good to know, thanks. I've been using Idea and Resharper for too long to change at this point, but it's good to know there are free alternatives for others :) – Sean Feb 05 '11 at 16:21
  • Yeah areas don't really fit the bill here.. as for the tool those might ease the pain, but I feel like LoginModel and LoginView have a lot more in common then say LoginView and ProductView, so would be nice to be able to out them in the same folder :) – t0PPy Feb 05 '11 at 21:13
  • 1
    So far as the scripts go, it seems you can just drop them in with the views.. http://www.lazycoder.com/weblog/2009/03/17/aspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files/ – t0PPy Feb 05 '11 at 21:17
  • Don't forget VS (at least 2010) has "Go to Controller" from a View, and "Go to View" from a Controller action in the context menus. This can help navigation. – Matt Greer Feb 09 '11 at 15:36

5 Answers5

2

I know exactly what you're talking about. Here are the conditions where I find the default MVC folder structure to be onerous:

  • I'm using a model-per-view approach
  • My controller basically only works with that one particular view
  • I have some javascript that only pertains to that view

Why do I want to put each of these pieces in a different folder?

I create a folder for the view in the Views folder, so you have a folder ~/Views/MyEntityList (just like the traditional MVC approach), but I put everything that pertains to that component there:

  ~/Views/MyEntityList/
       MyEntityListController.cs
       MyEntityListModel.cs
       MyEntityList.js
       MyEntityList.aspx

I find this structure leads all the developers to keep views decoupled from one another. No special MVC configuration is required, except for allowing browsers to access the .js resources directly.

There are some architectural patterns where this might not be a good way to go. For a model-per-view approach (see Los Techies for more description) I really like this structure.

pettys
  • 2,293
  • 26
  • 38
1

I think you need to get the Solution Navigator extensions via Power Tools update for VS 2010.

That way, you can display in the Solution Navigator, as opposed to the solution explorer, only the open files, for example. Makes it easier.

By the way, delete all the model folders and create a separate model project, eg:

MyApp.Domain

Any solution that is beyond basic will benefit from this.

As stated in the comments to your question, Areas will also reduce your navigation requirements.

awrigley
  • 13,481
  • 10
  • 83
  • 129
1

The only "looking of files" going on is with views, everything else is just a convention, so if you want you could have:

Directory: /Membership

  • MembershipController
  • LogOnView
  • LogOnModel
  • RegisterView
  • RegisterModel

... but the views must be in ~/Views/Membership

Max Toro
  • 28,282
  • 11
  • 76
  • 114
  • No, Views can be anywhere. You just don't get the nice path searching you get normally. You can specify any path you like to anywhere for your view. – Erik Funkenbusch Feb 09 '11 at 15:51
  • @Mystere Man: Yes, you can use absolute paths. I'm talking about how the framework locates stuff, so unless you use an absolute path views must be in `~/Views`. – Max Toro Feb 09 '11 at 17:22
0

One way I can think of to achieve this is by writing your custom view engine. You can place all these below files in Controllers/Membership

  • MembershipController
  • LogOnView
  • LogOnModel
  • RegisterView
  • RegisterModel

Models will not be a problem you can simply change the namespace for the models, the only problem is with the views. For this write your custom view engine so that your mvc application knows the physical location of the view files as follows.

 public class CustomViewEngine : RazorViewEngine
 {
    public CustomViewEngine()
    {
        ViewLocationFormats = new[]
         {
            "~/Controllers/{1}/{0}.cshtml",

        };
    }
 }

In global.asax.cs add the ViewEngine in Application_Start() by including the following code

 ViewEngines.Engines.Clear();
 ViewEngines.Engines.Add(new CustomViewEngine());

You may also have to take care of various other factors like updating the Layout attribute depending on where you place the _Layout.cshtml.

In case you are using areas, add the AreaViewLocationFormats string array as well.

You can do further customization by overriding some of the methods like FileExists, CreateView, CreatePartialView.

Note: Do not forget to copy web.config in the views folder to the Membership controller. Otherwise application does not find the required mvc namespaces and it does not find the symbols like viewbag, model etc.

jonni
  • 338
  • 1
  • 13
0

It looks like you have to override some behavior in the view engine. You can See this question to get a better idea.

Community
  • 1
  • 1
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291