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

8
  • I think you are interested in MVC Areas: msdn.microsoft.com/en-us/library/ee671793.aspx Commented Feb 5, 2011 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. Commented Feb 5, 2011 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 :-/ Commented Feb 5, 2011 at 15:10
  • @Sean DPack does the same thing with alt-u. It is free and works with scripts too. Commented Feb 5, 2011 at 15:50
  • 1
    So far as the scripts go, it seems you can just drop them in with the views.. lazycoder.com/weblog/2009/03/17/… Commented Feb 5, 2011 at 21:17

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

2 Comments

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.
@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.
0

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

Comments

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.