简体   繁体   中英

Change MVC folders structure in Visual Studio 2017

Well, Im trying to develop a web project that will initially contain 2 modules (applications). For these applications is required to have the same base URL. So, it would be:

baseurlexample.com/module1/index

baseurlexample.com/module2/index

What Im trying to do is to create one single application with this scenario structured inside it. Something like that:

\ Project
..\ Application
..\..\ SharedClass
..\..\ Modules
..\..\..\ Module1
..\..\..\..\ Models
..\..\..\..\ Controllers
..\..\..\..\ Views
..\..\..\ Module2
..\..\..\..\ Models
..\..\..\..\ Controllers
..\..\..\..\ Views

The problem is that the standard MVC scaffolding configurations won't let me do that. Everytime I create a Controller and try to add a View from that Controller, it will be created in the standard View folder.

Is there a quick way to fix that?

Is there a best approach for that, like creating two applications and deal with the routes later in the IIS?

Looks like your looking for feature folder based folder organization. For controllers it is pretty straightforward as it is cs file. For views however you need to write new viewfinder engine.

Here is an example,

public class FeatureFoldersRazorViewEngine : RazorViewEngine
{
    public FeatureFoldersRazorViewEngine()
    {
        var featureFolderViewLocationFormats = new[]
        {
            "~/Features/{1}/{0}.cshtml",
            "~/Features/{1}/{0}.vbhtml",
            "~/Features/Shared/{0}.cshtml",
            "~/Features/Shared/{0}.vbhtml",
        };

        ViewLocationFormats = featureFolderViewLocationFormats;
        MasterLocationFormats = featureFolderViewLocationFormats;
        PartialViewLocationFormats = featureFolderViewLocationFormats;
    }
}

Next, we have to add our newly created FeatureFoldersRazorViewEngine in our application.

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // ...
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new FeatureFoldersRazorViewEngine());
    }
}

For more, just google "Feature Folder in Asp"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM