更改ASP.NET MVC 3文件夹结构

Hry*_*rii 13 razor asp.net-mvc-3

我有兴趣更改结构文件夹.我看过很多文章,但我还没有找到解决方案.

我想这样做是为了在专题文件夹上分发文件和文件夹.我从RazorViewEngine创建了一个基类BaseViewEngine

public class BaseViewEngine : RazorViewEngine
    {
        public BaseViewEngine()
        {
            MasterLocationFormats = new[]
                                    {
                                        "~/Themes/My/master.cshtml"
                                    };

            ViewLocationFormats = new[]
                                    {
                                        "~/Modules/{1}/{0}.cshtml"
                                    };


            PartialViewLocationFormats = new[]
                                    {
                                        "~/Blocks/{0}.cshtml"
                                    };
        }
    }
Run Code Online (Sandbox Code Playgroud)

但它没有用.

更新

控制是原始的.仅用于测试

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var test = new Test { Text = "Hello" };
            return View(test);
        }

    }
Run Code Online (Sandbox Code Playgroud)

和观点

@model DemoModules.Test


<h2>Index</h2>
Run Code Online (Sandbox Code Playgroud)

但是当我运行项目时.我收到错误

CS0103:当前上下文中不存在"模型"的名称

关于结构文件夹,请参阅主题来源

Bru*_*oLM 8

您实际上不必实现新引擎来更改路径,您可以根据需要注册它们:

private static void RegisterViewEngines(ICollection<IViewEngine> engines)
{
    engines.Clear();

    engines.Add(new RazorViewEngine
    {
        MasterLocationFormats = new[] { "~/Themes/My/master.cshtml" },
        ViewLocationFormats = new[] { "~/Modules/{1}/{0}.cshtml" },
        PartialViewLocationFormats = new[] { "~/Blocks/{0}.cshtml" },
    });
}

protected void Application_Start()
{
    RegisterViewEngines(ViewEngines.Engines);
}
Run Code Online (Sandbox Code Playgroud)

作为参考,默认路径如下(不包括区域):

ViewLocationFormats = new [] {
  "~/Views/{1}/{0}.cshtml",
  "~/Views/{1}/{0}.vbhtml",
  "~/Views/Shared/{0}.cshtml",
  "~/Views/Shared/{0}.vbhtml"
};

MasterLocationFormats = new [] {
  "~/Views/{1}/{0}.cshtml",
  "~/Views/{1}/{0}.vbhtml",
  "~/Views/Shared/{0}.cshtml",
  "~/Views/Shared/{0}.vbhtml"
};

PartialViewLocationFormats = new [] {
  "~/Views/{1}/{0}.cshtml",
  "~/Views/{1}/{0}.vbhtml",
  "~/Views/Shared/{0}.cshtml",
  "~/Views/Shared/{0}.vbhtml"
};
Run Code Online (Sandbox Code Playgroud)


Tom*_*son 2

查看默认 Views 文件夹中的 web.config 文件。其中有一些 Razor 视图工作所需的东西 - 特别是视图的基类和将用于编译视图的命名空间。