开发者

Changing ASP.Net MVC directory structure

开发者 https://www.devze.com 2023-02-09 06:06 出处:网络
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 directo开发者_如

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 directo开发者_如何学Pythonry 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


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.


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消