在Visual Studio 2017中更改MVC文件夹结构

时间:2018-08-27 16:52:59

标签: c# asp.net asp.net-mvc asp.net-mvc-scaffolding

嗯,我正在尝试开发一个最初包含2个模块(应用程序)的Web项目。对于这些应用程序,必须具有相同的基本URL。因此,它将是:

baseurlexample.com/module1/index

baseurlexample.com/module2/index

Im试图做的是创建一个内部结构化的单个应用程序。像这样:

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

问题在于标准的MVC脚手架配置不允许我这样做。每当我创建一个Controller并尝试从该Controller添加一个View时,它将在标准View文件夹中创建。

是否有解决此问题的快捷方法?

是否有最好的方法,例如创建两个应用程序并稍后在IIS中处理路由?

1 个答案:

答案 0 :(得分:0)

看起来像您在寻找基于功能文件夹的文件夹组织。对于控制器,它非常简单,因为它是cs文件。对于视图,您需要编写新的取景器引擎。

这里是一个例子,

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;
    }
}

下一步,我们必须在应用程序中添加新创建的FeatureFoldersRazorViewEngine。

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

有关更多信息,请仅在Google中使用“ Asp功能文件夹”