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

时间:2011-06-13 11:14:00

标签: asp.net-mvc-3 razor

我对更改结构文件夹感兴趣。我读了很多文章,但我还没有找到解决方案。

我想这样做是为了在主题文件夹中分发文件和文件夹。我从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"
                                    };
        }
    }

但它没有用。

更新

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

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

    }

并查看

@model DemoModules.Test


<h2>Index</h2>

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

  

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

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

3 个答案:

答案 0 :(得分: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);
}

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

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

答案 1 :(得分:7)

答案 2 :(得分:2)

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